I'd like to find out if it is possible to implement a navigation system where one child activity can have two parent activities. Basically, I have a stream of content which a user may favourite. They can share a saved item via email, from both the stream activity and an activity which displays "favourited" content. I want to avoid duplicating a class simply because of navigation.
Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.
You can use a Fragment API to complete the task. See full details in developer's guide. Then create a MyFragment class and load it when appropriate.
As per docs -> section android:parentActivityName : The system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder .
Simply call your parent activity using getActivity() method.
Yes, it is possible. But in case of 2 or more parents you cant rely on the implementation of Up Navigation as described here: Providing Up Navigation
So, you are left with 2 options:
1- Use the back button behavior
You can do this by just calling finish()
or onBackPressed()
in your onOptionsItemSelected(MenuItem item)
's android.R.id.home
case. Like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
2- Go back to the first activity of your app
Take the user back to first activity from where your app starts, like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent backIntent = new Intent(this, YOUR_FIRST_ACTIVITY.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(backIntent);
return true;
}
By the way, this question is a possible duplicate of this question
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