Message-Fragment
Activity-A
Camera-Fragment
Message-Fragment
Camera-Fragment
ImagePreview-Fragment
VideoPreview-Activity
VideoTrim-Activity
VideoPreview-Activity
VideoPreview-Activity
ImagePreview-Fragment
Message-Fragment
Message-Fragment
VideoPreview-Activity
VideoPreview-Activity
VideoTrim-Activity
ImagePreview-Fragment
Message-Fragment
addToBackStack(fragmentTag)
If I understand everything correctly I think I have a solution. I will ignore all other aspects of the navigation and just focus on the fact you want to go from VideoPreviewActivity
to the MainActivity
and adjust the fragment stack, and that this is being triggered from a done button (or something similar) but NOT the back button.
The simple way would be to use the activity result functions startActivityForResult
which would allow you to use a onActivityResult
function in the main activity to pass the information back. However you may potentially navigate to the VideoPreviewActivity
via the VideoTrimActivity
so you would have to pass this information back through two activities which would be a bit messy.
So my proposed solution has a few steps.
Instead of calling finish()
on the VideoPreviewActivity
fire an intent to create the MainActivity
Intent intent = new Intent(context, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra()//Add your return data here
startActivity(intent)
Instead of starting a new MainActivity
this will return you to the previous one and finish all the activities sat above it in the task task.
Add this function to the MainActivity
.
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
Then in the onStart
function you can check your intent to see if there is data in there returned from VideoPreview
and remove the fragments you no longer need.
if (getIntent().hasExtra(YOUR_DATA)) {
if (getSupportFragmentManager().getBackStackEntryCount() > 0)
getSupportFragmentManager(). popBackStackImmediate("MESSAGE_TAG", 0);
}
Where "MESSAGE_TAG" is the tag used in the ft.addToBackStack("MESSAGE_TAG")
call.
This will clear all of the fragments with an addToBackStack
in the transaction. The MessageFragment
will start its lifecycle calls again and you can access the data from
getActivity().getIntent()