Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

Android - Overriding ActionBar back and device back button

In my app I have a MainActivity and a TimerActivity. In normal circumstances in TimerActivity the device back button and the ActionBar's up button work as they should - they lead from the TimerActivity to the MainActivity. But when I open the TimerActivity by clicking on my app's notification, the back buttons lead to the home screen instead of the MainActivity. I would like both back buttons (device and ActionBar's up button) to always open the MainActivity - unless of course the user is in the MainActivity in which case the back button should close the MainActivity. This is how Gmail and Google Drive apps work when you open an activity through a notification and it makes the most sense.

Here's how my notification opens the activity:

Notification timerNotification;

mBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(ongoingNotificationIcon)
    .setContentTitle(ongoingNotificationContentTitle)
    .setContentText(ongoingNotificationContentText)
    .setTicker(ongoingNotificationTicker)
    .setPriority(99)
    .setOngoing(true);

Intent resultIntent = new Intent(this, TimerActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
        this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
timerNotification = mBuilder.build();

startForeground(MyApplication.NOTIFICATION_ID, timerNotification);

I tried overriding the back button in TimerActivity like this:

public void onBackPressed() {    
    Intent intent_main = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent_main);
}

But then pressing the back button again on the MainActivity returns the user to the TimerActivity (so the user is in a loop) instead of exiting the app which is the desired behavior. Also, onBackPressed() doesn't affect the ActionBar's up button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. To override Actionbar Up button, use:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch(item.getItemId()) {
            case android.R.id.home:
                //User clicked home, do whatever you want
                return true;
            default:        
                return super.onOptionsItemSelected(item);
        }
    }
    
  2. To auto return to previous activity, please specify previous activity in AndroidManifest.xml:

    <activity
        android:name=".SecondActivity"              
        android:label="@string/label_myLabel"
        android:parentActivityName=".FirstActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.yourpackage.FirstActivity"/>
    </activity>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...