Preserve state between activities - android

I have two different activities, ActA which is my main Activity, and ActB. I get to ActB from ActA through a menu voice. If I press the back button (either software or hardware) in ActB the app come back to ActA preserving my previous state in this activity. If instead I press the up button in the activity bar, the ActA reset its state. How can I prevent this to happen?
Don't know if this could ever help, but this is some code:
Activity A
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_save:
startActivity (new Intent (this, Save.class));
return true;
case R.id.action_about:
}
return false;
}
Manifest for Activity B:
<activity
android:name="com.myapplication2.app.Save"
android:label="#string/title_activity_save"
android:parentActivityName="com.myapplication2.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapplication2.app.MainActivity" />
</activity>

Add this switch case:
switch (item.getItemId()) {
case android.R.id.home: finish(); return true;
}
The way it works, Up (android.R.id.home) by default calls your Activity with the same Intent, and depending on how you handled onCreate and onResume it may be the case that you're recreating your views. If you change Up behaviour to match Back the state is preserved.
You won't need android:parentActivityName or meta-data, just add
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
somewhere on your lifecycle code.

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?

How to detect if the Up button was pressed

In my activity the action bar shows only the left arrow and the title of the activity.
When I press the left arrow the activity goes back to the previous activity, but no event is registered in the onKeyUp, OnkeyDown and OnBackPressed methods.
But when I press the Back key on the phone (at the bottom) the activity goes back to the previous one and all the methods onKeyUp, OnKeyDown and OnBackPressed register an event (in the logcat).
How can I capture that left arrow (I think it is called the UP button)?
The reason I need to capture the key is to know in the onPause method that the activity is destroyed by the user and not by the system (for example, if the user switches to another activity).
By further investigating he matter I found out that the UP button gives an event that is captured by the onOptionsItemSelected method and since there is no other button on the menu I know it is this button.
see http://developer.android.com/guide/topics/ui/actionbar.html#Handling
Handling clicks on action items
When the user presses an action, the system calls your activity's onOptionsItemSelected() method. Using the MenuItem passed to this method, you can identify the action by calling getItemId(). This returns the unique ID provided by the tag's id attribute so you can perform the appropriate action. For example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
onUpButtonPressed();
return true;
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Note: If you inflate menu items from a fragment, via the Fragment
class's onCreateOptionsMenu() callback, the system calls
onOptionsItemSelected() for that fragment when the user selects one of
those items. However, the activity gets a chance to handle the event
first, so the system first calls onOptionsItemSelected() on the
activity, before calling the same callback for the fragment. To ensure
that any fragments in the activity also have a chance to handle the
callback, always pass the call to the superclass as the default
behavior instead of returning false when you do not handle the item.
To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
Yes you are right, you can detect if the up button was pressed in the onOptionsItemSelected method. This should work:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Do something here. This is the event fired when up button is pressed.
return true;
}
return super.onOptionsItemSelected(item);
}

Activity Up-Navigation don't work

I have an Activity which should be able to navigate up to its parent. This is how I declared it in the Manifest:
<activity
android:name="com.myapp.SeminarActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:theme="#style/Theme.AppCompat.Light"
android:parentActivityName="com.myapp.SeminarOverviewActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapp.SeminarOverviewActivity" />
</activity>
It works when I enter SeminarActivity via an Intent in SeminarOverviewActivity. But it is possible to enter SeminarActivity from an other Activity. This activity has noHistory=truebecause I do not want to go back to it via Up-Navigation. When I enter SeminarActivity form this Activity and Press back, the App ends. I also tried use NavUtils and added this from the android documentation:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
}
return super.onOptionsItemSelected(item);
}
but no effect. In my tests, always the else part of the above snipped is used.

Up Navigation is not working

So I have a very simple project. It contains two activities. The main activity has a navigation drawer and a fragment container. The second activity is merely meant to display details when the user interacts with a certain fragment.
So I have set my main activity as the parent activity to my second activity (called DetailsPage) like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsPage"
android:label="#string/title_activity_details_page"
android:parentActivityName="com.collusion.serviceassistant.MainActivity" >>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.collusion.srviceassistant.MainActivity"/>
</activity>
and in the DetailsPage activity code I have the following:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.share :
share();
return true;
case R.id.home:
Log.i("BACK", "Going back!");
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new
// task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
Now it seems that by my log, the code that pertains to the up action is not getting executed. Whenever I hit the up caret in the action bar or use the back hardware key, the app simply exits. Does anyone have any idea what I am doing wrong here? I am wondering if it has anything to do with the fact that the details page activity does not extend the ActionBar class:
public class DetailsPage extends Activity{
Does anyone have any ideas?
According to Google docs link on navigation using the home button you might want to use
android.R.id.home instead of R.id.home
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
I think this might solve the home up navigation.

Up Button in ActionBar with the possibility to move more activities

I'm doing an app for Android. In the DetailActivity, there is the ActionBar with the Up Button. When I press it, it would return in the previous activity. But the DetailActivity can be launched from different activity.
Instead of using NavUtils.navigateUpTo(this, upIntent) , you can just use finish() or onBackPressed(). This would finish the activity, and go back to the previous one. So handling of the Up button being pressed would look like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);

Categories

Resources