Android from Fragment to Activity, back press dont go back to fragemt - java

I am loading an activity from my fragment. If I press the back button on the device, it goes back to the Gridfragment (as expected).
But, if I click the action bar back arrow, it goes back to the mainActivity hosted fragment = HomeFragment and not the Gridfragment.
From HomeFragment - ( default fragment)
Fragment f = GridFragment.newInstance(GridFragment.TAG_bundlePhotosList);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).addToBackStack(null).commit();
Fragment Grid:
Intent i = new Intent(getActivity(), ImagePreviewActivity.class);
i.putExtra("from_grid", "from_grid");
tartActivity(i);
ImagePreviewActivity :
//Display back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(ImagePreviewActivity);

This is the same problem, and as such the same solution as here.
When you use UP navigation, then the previous activity is recreated. To prevent that from happening while you preserve the UP navigation, you can get the intent of the parent activity, and bring it to front if it exists, otherwise create it if not.

Related

Call Main activity from a Fragment

fragment class
Home/main activity
How to limit my main activity to not go back on the last page of my Fragment once return from the last page of Fragment to Main activity like I have One Home activity and that home activity leads to 3 more activities on clicking the button and in which one activity has fragments. The last page of the fragment is leading to home activity but when I back pressed on the home activity it again leads me to the last page of a fragment from where I've come and then I press the back button again then my application shuts
When you are adding your Fragments to the FragmentManager make sure to avoid adding it to the backstack, if you don't want it to appear on its own.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_layout,fragment);
//Adding null will stop the fragment from reappearing on a backpress.
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
In the future please post code in text format. Do not post images of code. If you need to show some view behavior on the screen then its fine to post images, but not code.

PopBackStack previous last fragment and check which fragment it is

I got a few fragments in MainActivity which can be selected from Navigation Drawer. Now, whenever users press a profile button, it will jumps to UserProfile Fragment. If home button is pressed, it will pop back the last fragment. Since I've assigned each of the fragments a specific backstack name i.e .addToBackStack("abc"), how can I check what is the last fragment by using popBackStack() method ?
To get the last fragment:
FragmentManager fm = getSupportFragmentManager();
int lastFragEntry = fm.getBackStackEntryCount()-1;
String lastFragTag = fm.getBackStackEntryAt(lastFragEntry).getName();
Log.i("Last Fragment Tag->", lastFragTag);
NB: If you want to get the name/tag of last fragment, you also have to use the same Tag during fragment transaction:
ft.replace(android.R.id.container, fragment, "abc");
ft.addToBackStack("abc");
Hope this helps.

Syntax for image button

How do I make the image button change to different fragment on click?
On the homepage fragment I have an image button. How do I code it so that it changes to a different fragment when clicked. Note that I have java classes for all my fragments as well as the main activity class.
Where would I put the code for this image button and how would I do it because everything I have tried doesn't work.
You can create onClickListener for the image button in home fragment and inside onclick:
Fragment fragment = new {your-fragment()};
FragmentManager fm1 = getActivity().getSupportFragmentManager();
FragmentTransaction ft1 = fm1.beginTransaction();
fm1.popBackStack();
ft1.replace({layout}, fragment).addToBackStack("tag");
ft1.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft1.commit();
Hope this helps

Android: add two or more fragments to the same backstack state

I have a main activity with a RelativeLayout container where i load different fragments using theFragmentManager.
Let's say i load fragment1 and fragment2 with two transactions. In case of back button pressed, i want to return to the state when both fragment1 and fragment2 were not loaded. Is this possible?
I've tried to use the same name in the addToBackStack() method:
mFragmentManager.beginTransaction().replace(R.id.activity_main, fragment1).addToBackStack("SameState").commit();
mFragmentManager.beginTransaction().add(R.id.activity_main, fragment2).addToBackStack("SameState").commit();
but this doesn't work. If i press the back button fragment2 disappears first, then pressing again fragment1 goes away too. Any suggestion?
Try this:
mFragmentManager.beginTransaction()
.replace(R.id.activity_main,fragment1)
.addToBackStack(null)
.commit();
getSupportFragmentManager().popBackStack(); // Pop the first transaction
mFragmentManager.beginTransaction()
.replace(R.id.activity_main,fragment2)
.addToBackStack(null)
.commit();
At this point, when you press the back button, it will simply revert the last transaction (fragment2 replacing).

Why my back button does nothing?

I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack

Categories

Resources