Android - Navigation Up from Activity to Fragment - android

I'm developing some application and I have one problem.
I have :
1. Activity A (Navigation Drawer pattern) with ListFragment in FrameLayout:
xml:
<FrameLayout
...>
</FrameLayout>
<LinearLayout
...>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Activity B which shows the detail data of ListView in ListFragment.
How can I go back (using Navigation Up Button) from activity B to Activity A with saving UI of the ListFragment (Activity re-creates if I go back using Home Back).
Btw, if I press the back button on my phone, activity does not re-create and returns in previous state.

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.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentIntent = NavUtils.getParentActivityIntent(this);
parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(parentIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I also specified launchMode="singleTop" in the Manifest. but I am not sure if that was necessary.

One thing you can do to prevent the first activity to recreate is by just calling finish() on the second activity when that back button is pressed.
Not tested, but I believe the id is android.R.id.home, so all you have to do is override onOptionsItemSelected in the second activity, like this:
/**
* Handles the selection of a MenuItem.
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Related

Navigate up not working

I have three activities A->B->C. Sometimes I launch Activity C directly from A. Even then, when the user presses the "up" button, I want to go to activity B. So after a lot of searching, I am doing this:
AndroidManifest.xml
<activity
android:name=".B"
android:parentActivityName=".A"
android:launchMode="singleTop"/>
<activity
android:name=".C"
android:parentActivityName=".B"/>
In Activity C:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this,upIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But when I start activity C from A, the up button just goes back to A, instead of creating a new instance of B.
Even the android docs state:
When you call this method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward.
Am I missing something here?

Action bar back button from un activity to previous activity fragment

I'm using a navigation drawer in a main activity with many fragments
For example :
I have fragment 1 for presentation
And fragment2 that contains a listView.
If I click an item in the listView it will launch another activity
When i click the action bar back button i want to go to the main activity fragment2
But the fragment1 is used
How can I use the second fragment
Thanks for your answers
In your second activity's onCreate, put(although i think you already have) :
getActionBar().setDisplayHomeAsUpEnabled(true);
and inside second activity's options handling(i think you've done this too) :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//perhaps use intent if needed but i'm sure there's a specific intent action for up you can use to handle
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and inside your first activity's onCreate's intent handler for example :
if(Intent.ACTION_VIEW.equals(action))
you set the current page of the viewpager with viewpager.setCurrentItem(position). If you want to change the page for different items you could use intent.putExtra() in your second activity and then retrieve it in your first activity.
I have added the code below in my second activity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
onBackPressed();
return true;
}
It's working as I expected
is it a clean solution ?
Thanks

Android activity go back to activity that started it instead of parent activity when pressing navigation bar back button

I have the following scenario:
In android manifest I have three activities:
ActivityA
ActivityB - parent of ActivityA
ActivityC
What I want to do is start ActivityA from ActivityC using intent.StartActivity(). The activity is started successfully. Now I want to go back to ActivityC using actionbar's back button (upper left corner), but since ActivityA has ActivityB as parent (as declared in android manifest) the actionbar back button takes me to ActivityB instead of previous ActivityC. If I use the back keyboard button, I get redirected to the ActivityC.
What can I do to get the same result in both "navigate back" cases. The result I'm seeking is to get redirected to the activity that started the ActivityA and not it's parent activity. Is it possible?
You should not define ActivityB as parent for ActivityA in manifest. Instead, handle onOptionsItemSelected in ActivityA like this:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
}
return super.onOptionsItemSelected(item);
}
When you call startActivity(), do it like that:
Intent intent = new Intent(callingActivity.this, destinationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Try the following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
This will emulate a back button press, which will additionally preserve the state of the Activity you came from (tabs, scroll position).
(Credit goes to #Kise for suggesting this in https://stackoverflow.com/a/31331757/2703209)

How to navigate up to the same parent state

From my observation from Gmail and TED app the behavior of up navigation it will navigate to parent with the same state (scroll position) not like what Google say in their doc Implement Up Navigation which like create a parent intent and start it.
I implement the code from Android sample code and all state are gone (All Extra parameters I have previously set and scroll position). What is the proper way on this ? I can't find any on Android document.
Below is the code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, MyParentActivity.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is not part of the application's task, so create a new task
// with a synthesized back stack.
TaskStackBuilder.from(this)
.addNextIntent(new Intent(this, MyGreatGrandParentActivity.class))
.addNextIntent(new Intent(this, MyGrandParentActivity.class))
.addNextIntent(upIntent)
.startActivities();
finish();
} else {
// This activity is part of the application's task, so simply
// navigate up to the hierarchical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
In my case I got 3 activities say A B and C, when user navigate from A to B I put some extras and onCreate of B I use that extras to query data from database to populate my rows and when I navigate back from C all extras are gone and Activity B show nothing.
The "standard" behaviour for an android activity is, that a new instance of the activity is created, every time there is a new intent for this activity (see launchMode-docu here). Because of this your extras seem to be gone, if you call navigateUpTo.
In your case, I would advise to use
android:launchMode="singleTop"
for your parent activity in your AndroidManifest.xml. This way you will return to your existing activity (as long as it is on the top of the back stack of your task). This way your extras will be preserved.
I too, do not understand why this is not mentioned in the Google doc you cite, as this seems the behaviour one expects if using up-navigation.
This is an alternative solution to the accepted answer:
If you cannot change the launchMode of your activity or if the parent activity is not on top of the back stack (e.g. A is parent of C), you cannot use the solution above. In this case you have to extend your navigateUpTo call to tell the activity, that it should not be recreated, if it is on the back stack:
Intent intent = NavUtils.getParentActivityIntent(this);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(this, intent);
I had a similar issue when I called startActivityForResult() in my main activity with fragments and then tried to return to it from the callee using Up navigation.
Solved by implementing Up navigation as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
setResult(RESULT_CANCELED);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In this case Up button behaves like an ordinary Back button and all states are preserved.
you can use this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
You would need to save the state on the parent activity and recover it after returning from the calling one.
See Saving Android Activity state using Save Instance State for a complete explanation of the preocess, with code.

Android Actionbar Up button versus system Back button

I'm using the Actionbar and it's "up" button to return from a detail activity to the main activity, which works fine. Similarly, the user can press the system "back" button to return to the main activity.
In my main activity, in onCreate() data is downloaded from the internet to display upon app start. I noticed that when I use the Actionbar "up" button to go from detail to main activity, onCreate() is run, re-downloading the data. But onCreate() is not run when I use the system "back" button, therefore immediately showing the main activity view.
The code I use in the detail activity to implement the "up" button is:
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
I would like the "up" button to behave like the "back" button and not rerun onCreate(). But I'm unsure how to make this happen, or which code path the "back" button implements to return to the main activity.
Thanks!
Instead of starting a whole new activity simply finish the details activity you are in
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
Then you will return to the previous activity on the activity stack (your main activity) and onCreate shouldn't be called
If you want Up to do exactly what Back does, you can do this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Note that the default implementation of onBackPressed() just calls finish(), but onBackPressed can be overridden.
I think a better solution can be found in this post.
Calling finish() works in specific situations but may not always produce the behavior described in the documentation e.g:
By calling
Intent intent = NavUtils.getParentActivityIntent(this);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(this, intent);
you'll return to the parent activity in the state in which you left it. If you have a flat app structure, it'll still act just like the Back button.
for a real "home" functionality , you should see the api demos ,under "App/Activity/Reorder Activities" .
the reason : what if you have something like this : activity1->activity2->activity3 , and now you wish to go to activity1 by pressing the home button on the action bar?
I believe the simplest way is to override the "getParentActivityIntent" method of the detail activity adding the flag "Intent.FLAG_ACTIVITY_CLEAR_TOP":
#Nullable
#Override
public Intent getParentActivityIntent() {
Intent intent = super.getParentActivityIntent();
if (intent != null) {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
return intent;
}
There is another solution which can be in hand for somebody. I had the same double-behavior when user pressed Back and ActionbarBack buttons. I was fine with Back btn behaviour. I didn't want an activity to be recreated. So I overrode the method
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
Works fine for me

Categories

Resources