Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,729,749 Members 49,754 Now Online
XDA Developers Android and Mobile Development Forum

[Q]Execute a method of fragment in host activity

Tip us?
 
arpitkh96
Old
#1  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 374
Posts: 1,083
Join Date: Feb 2012
Location: panipat
Default [Q]Execute a method of fragment in host activity

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
 
facetea
Old
#2  
Junior Member
Thanks Meter 1
Posts: 4
Join Date: May 2014
Quote:
Originally Posted by arpitkh96 View Post
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: [ Click to Expand ]
 
arpitkh96
Old
#3  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 374
Posts: 1,083
Join Date: Feb 2012
Location: panipat
Quote:
Originally Posted by facetea View Post
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
 
arpitkh96
Old
#4  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 374
Posts: 1,083
Join Date: Feb 2012
Location: panipat
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes