fthopkins fthopkins - 1 year ago 128
Android Question

Return back to specific fragment of Activity-A from another Activity

enter image description here

I open the

Message-Fragment
from
Activity-A
and then open the
Camera-Fragment
from
Message-Fragment
. Now current fragment is
Camera-Fragment
and i have media-slider on it. Now I have two options, one is
ImagePreview-Fragment
that is opened if I click the image item on media-slider. Other is
VideoPreview-Activity
that is opened if click the video item on media-slider. So there is one condition of clicking video-item. If I click the video-item on media-slider and video size is bigger than 10 MB, I open the
VideoTrim-Activity
and when trim is completed i call the
VideoPreview-Activity
.

I'm sending a media from
VideoPreview-Activity
or
ImagePreview-Fragment
. So when I send the media I want to return back to the
Message-Fragment
with data.

How can I return back to
Message-Fragment
from
VideoPreview-Activity
(opened from Camera-Fragment) and
VideoPreview-Activity
(opened from
VideoTrim-Activity
). Also if you can help me about how to return back from
ImagePreview-Fragment
to
Message-Fragment
I would be much appreciated. These are seperated questions. Hope you can help.

I open the fragments with adding to backstack
addToBackStack(fragmentTag)
.

Edited : VideoPreview-Fragment must be VideoPreview-Activity
and VidoTrim-Fragment must be VideoTrim-Activity. these are the activities. like you see on the image.

Answer Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download