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.
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()
Try this,
First take the framelayout in ActivityA,
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Create method in activityA
public void openFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, null).commit();
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment, backStateName);
fragmentTransaction.addToBackStack(backStateName);
fragmentTransaction.commit();
}
After this done in your mainactivity and then overide the method like
@Override
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
if (exit) {
finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 2 * 1000);
}
} else {
getSupportFragmentManager().popBackStackImmediate();
int c = getSupportFragmentManager().getBackStackEntryCount();
}
}
Use in this way,
Fragment fragment;
fragment = new FS_PLKit_Calculator();
((FS_Home) getActivity()).openFragment(fragment);
try this way you can maintain backstack of your flow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With