Android的 - 重写动作条背部和设备后退按钮 [英] Android - Overriding ActionBar back and device back button

查看:161
本文介绍了Android的 - 重写动作条背部和设备后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有一个MainActivity和TimerActivity。
在TimerActivity正常情况下该设备后退按钮和动作条的向上按钮的工作,因为他们应该 - 他们从TimerActivity到在MainActivity领先。但是,当我点击我的应用程序的通知打开TimerActivity,背部按钮导致主屏幕,而不是在MainActivity。
我想这两个后退按钮(设备和ActionBar的向上按钮)总是开在MainActivity - 当然,除非用户在这种情况下后退按钮应该关闭在MainActivity在MainActivity。这是当你通过通知打开一个活动,它使最有意义的Gmail和谷歌云端硬盘应用工作。

下面是如何我通知打开的活动:

 通知timerNotification;mBuilder =新NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(ongoingNotificationIcon)
    .setContentTitle(ongoingNotificationContentTitle)
    .setContentText(ongoingNotificationContentText)
    .setTicker(ongoingNotificationTicker)
    .setPriority(99)
    .setOngoing(真);意图resultIntent =新意图(这一点,TimerActivity.class);
的PendingIntent resultPendingIntent = PendingIntent.getActivity(
        此,0,resultIntent,PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
timerNotification = mBuilder.build();startForeground(MyApplication.NOTIFICATION_ID,timerNotification);

我试图重写在TimerActivity后退按钮这样的:

 公共无效onBack pressed(){
    意图int​​ent_main =新意图(getApplicationContext(),MainActivity.class);
    startActivity(intent_main);
}

但随后$ P $再次pssing上在MainActivity后退按钮返回用户到TimerActivity(因此用户是在一个循环中),而不是退出应用程序,它是所需的行为。此外, onBack pressed()不影响ActionBar的按钮即可。


解决方案

  

onBack pressed()不影响ActionBar的按钮即可。


后退按钮和向上按钮导航是两种不同的手段。

从文档

最多


  

向上按钮用于基础上,应用程序内导航
  屏幕之间的等级关系。


返回


  

系统返回按钮用于导航,按时间倒序
  为了通过屏幕史上用户最近的工作
  用。它通常是基于之间的时间关系
  屏幕上,而不是应用程序的层次结构。


那么,是什么你描述为你的问题其实是的建议的方式,通过您的应用程序导航。

不过,如果你希望你的用户去你的 MainActivity TimerActivity 之后pressing您通知,实现一个最简单的方法是将包括意图额外的,当你推出来检查活动通知

下面是一个例子:

 意图resultIntent =新意图(这一点,TimerActivity.class);
resultIntent.putExtra(fromNotification,真正的);

TimerActivity

 私人布尔mFromNotification;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    最终捆绑ARGS = getIntent()getExtras()。
    如果(参数!= NULL){
        mFromNotification = args.getBoolean(fromNotification);
    }
}@覆盖
公共无效onBack pressed(){
    如果(mFromNotification){
        startActivity(新意图(这一点,MainActivity.class));
        完();
    }其他{
        super.onBack pressed();
    }
}

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.

解决方案

onBackPressed() doesn't affect the ActionBar's up button.

The "back" button and the "up" button are two different means of navigation.

From the docs:

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 = new Intent(this, TimerActivity.class);
resultIntent.putExtra("fromNotification", true);

In your TimerActivity

private boolean mFromNotification;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Bundle args = getIntent().getExtras();
    if (args != null) {
        mFromNotification = args.getBoolean("fromNotification");
    }
}

@Override
public void onBackPressed() {
    if (mFromNotification) {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    } else {
        super.onBackPressed();
    }
}

这篇关于Android的 - 重写动作条背部和设备后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆