FORUMS

[Q]Execute a method of fragment in host activity

1,149 posts
Thanks Meter: 582
 
Post Reply Subscribe to Thread Email Thread
Basically what I want is to override back button behavior in fragment.I tried following code (in activity)but that didn't worked.
Code:
public void onBackPressed(){
String n="path"
new Main().loadlist(n);

}
here I am trying to execute the method loadlist of fragment "Main".so that it updates the list view in the fragment itself.please help me to accomplish this

Sent from my GT-S5570 using XDA Premium 4 mobile app
 
 
2nd May 2014, 04:30 PM |#2  
Junior Member
Thanks Meter: 1
 
More
Quote:
Originally Posted by arpitkh96

Basically what I want is to override back button behavior in fragment.I tried following code (in activity)but that didn't worked.

Code:
public void onBackPressed(){
String n="path"
new Main().loadlist(n);

}
here I am trying to execute the method loadlist of fragment "Main".so that it updates the list view in the fragment itself.please help me to accomplish this

Sent from my GT-S5570 using XDA Premium 4 mobile app

There two way
1.use broadcast, the host activity send broadcast and fragment recv it.
2.use FragmentManager to get the fragment via res id
FragmentManager fm = getSupportFragmentManager();
mainFragment = (MyFragment)fm.findFragmentById(R.id.mainfragment) ;
mainFragment.loadlist(n);
The Following User Says Thank You to facetea For This Useful Post: [ View ]
2nd May 2014, 05:56 PM |#3  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 582
 
Donate to Me
More
Quote:
Originally Posted by facetea

There two way
1.use broadcast, the host activity send broadcast and fragment recv it.
2.use FragmentManager to get the fragment via res id
FragmentManager fm = getSupportFragmentManager();
mainFragment = (MyFragment)fm.findFragmentById(R.id.mainfragment) ;
mainFragment.loadlist(n);

I tried your second way like this
Code:
FragmentManager fm=getFragmentManager();
Fragment main=fm.findFragmentById(R.id.main);
main.loadlist(n);
Or
Code:
FragmentManager fm=getFragmentManager();
Fragment main=(Fragment)fm.findFragmentById(R.id.main);
main.loadlist(n);
But it shows unknown method loadlist

Sent from my GT-S5570 using XDA Premium 4 mobile app
2nd May 2014, 06:01 PM |#4  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 582
 
Donate to Me
More
Code:
 FragmentManager fm=getFragmentManager();
Main main=(Main)fm.findFragmentById(R.id.main);
main.loadlist(n);
This worked thanks for the help

Sent from my GT-S5570 using XDA Premium 4 mobile app
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools
Display Modes