Reputation:
I have one fragment in which a listview is created. When the list is clicked the same list is refreshed and its child elements are loaded on the same. Could anyone please tell me how to enable the backbutton in android to get the parent list?
Upvotes: 0
Views: 81
Reputation: 1401
Use the method onResume()
@Override
public void onResume() { // TODO Auto-generated method stub
super.onResume();
}
Upvotes: 0
Reputation: 960
For your simplicity, Define one static variable in your activity that track the which fragment is currently visible, like
public static fragIndex = 0;
then in your fragment oncreateView before returning view set it accordingly to fragments. like in list view fragment
MainActivity.fragIndex = 0;
and for detailed fragment
MainActivity.fragIndex = 1;
now on your activities onbackpressed method do like
if(fragIndex == 0){
//whatever you want to do.
}else if(fragIndex == 1){
// change the fragment back to list view
}
Upvotes: 0
Reputation: 397
You have to take callback from onBackPressed of Activity to Fragment and check in Fragment if there is child elements open or not.
In Activiity
public void onBackPressed()
{
if(!fragment.onBackPressed())//onBackPressed in fragment is your own function
{
super.onBackPressed();
}
}
In Fragment
public boolean onBackPressed() //your custom onBackPressed not overrided
{
if(childListOpen)
{
//close the child list
return true;
}
else
{
// whatever to do in back pressed of list
return false;
}
}
Upvotes: 0
Reputation: 274
You can override your activity's onBackPressed() and get the fragment instance by FragmentManager, and do what you want.
Upvotes: 1