Android - Overriding Actionbar Back And Device Back Button
Solution 1:
To override Actionbar Up button, use:
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case android.R.id.home: //User clicked home, do whatever you wantreturntrue; default: returnsuper.onOptionsItemSelected(item); } }
To auto return to previous activity, please specify previous activity in AndroidManifest.xml:
<activityandroid:name=".SecondActivity"android:label="@string/label_myLabel"android:parentActivityName=".FirstActivity"><meta-dataandroid:name="android.support.PARENT_ACTIVITY"android:value="com.yourpackage.FirstActivity"/></activity>
Solution 2:
onBackPressed() doesn't affect the ActionBar's up button.
The "back" button and the "up" button are two different means of navigation.
Up
The Up button is used to navigate within an app based on the hierarchical relationships between screens.
Back
The system Back button is used to navigate, in reverse chronological order, through the history of screens the user has recently worked with. It is generally based on the temporal relationships between screens, rather than the app's hierarchy.
So, what you're describing as your problem is actually the recommended way to navigate through your app.
Nevertheless, if you want your users to go to your MainActivity
from your TimerActivity
after pressing your Notification
, the easiest way to implement that would be to include an Intent
extra to check when you're launching the Activity
from your Notification
.
Here's an example:
Intent resultIntent = newIntent(this, TimerActivity.class);
resultIntent.putExtra("fromNotification", true);
In your TimerActivity
privateboolean mFromNotification;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finalBundleargs= getIntent().getExtras();
if (args != null) {
mFromNotification = args.getBoolean("fromNotification");
}
}
@OverridepublicvoidonBackPressed() {
if (mFromNotification) {
startActivity(newIntent(this, MainActivity.class));
finish();
} else {
super.onBackPressed();
}
}
Solution 3:
As explained in the Notifications Setting up a regular activity PendingIntent, you should use TaskStackBuilder to build a synthetic back stack so that your back button functions as if the user had navigated to their normally:
...
IntentresultIntent=newIntent(this, ResultActivity.class);
TaskStackBuilderstackBuilder= TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stackPendingIntentresultPendingIntent=
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManagermNotificationManager=
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
The Action Bar Up action, as you noticed, does not differ whether you are coming from a notification or not (as expected if you've set it up correctly).
Post a Comment for "Android - Overriding Actionbar Back And Device Back Button"