Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q]Execute a method of fragment in host activity

OP arpitkh96

1st May 2014, 03:41 PM   |  #1  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
Donate to Me
More
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, 05:30 PM   |  #2  
Junior Member
Thanks Meter: 1
 
4 posts
Join Date:Joined: May 2014
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, 06:56 PM   |  #3  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
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, 07:01 PM   |  #4  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
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 Search this Thread
Search this Thread:

Advanced Search
Display Modes