Home键preSS行为 [英] Home key press behaviour

查看:185
本文介绍了Home键preSS行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发一个样本Android应用程序我已经构建了两个活动 1)活动1 2)活动2

现在活动二是前台活动,而活动1是背景之一。现在,用户presses Home键。该应用程序(即两个活动)dissappear。现在是我们重新开始,我们看到活动1作为前台活动的应用程序。我的问题是:

1)是否该平台维持在pressed home键任何历史记录? 2)我们如何把用户带到最后的发射活动上重新推出的应用程序?

解决方案

我已经竭尽全力来应付这一奇怪的行为进行了一个多月,但我终于找到了通过试验和错误的解释。

此行​​为,当您从Eclipse的应用程序发生时,通过命令行或如果您打开按钮安装(而不是完成按钮),应用程序和preSS为您安装它之后立即启动应用程序。

如果在这些情形之一的,你开始你的apllication,去活动1,然后到活动2,preSS HOME按钮,然后preSS应用程序图标,它会打开活动1的新实例。不要把我的话。只要preSS回来看看,它让你到你的活性2,你离开的时候你pressed HOME。

看来,发射活动是穿不上的活动堆栈如果应用程序开始的上方,这就是为什么它在应用程序的堆栈当前的活动之上创建的发射活动的一个新实例提到的那些方式之一。这在我看来就像一个错误。

因此​​,解决方法是退出应用程序,它第一次是从Eclipse或者命令行或打开按钮等开始,以pressing后退按钮,当需要多次,然后输入应用程序再次。从此,该行为将被预期。

编辑: 一个更好的解决方法是这样的:创建一个DummyActivity,并将它设置为主要切入点进入应用程序。此外,添加了标记安卓noHistory =真正的。该 DummyActivity 很简单,是这样的:

  

公共类DummyActivity延伸活动{

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    如果(!MyApplication.startedApp){
        意向意图=新的意图(这一点,HomeActivity.class);
        startActivity(意向);
    }

    完();
}}
 

所有MyApplication 是一个扩展android.app.Application和内部AndroidManifest.xml中定义的类。在 HomeActivity.class 你的的onCreate()方法内设置布尔字段 startedApp 为true。如果用户$ P $从屏幕psses回来,你需要将价值startedApp为false。

公共类HomeActivity延伸活动{

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    MyApplication.startedApp = TRUE;
}

@覆盖
公共无效onBack pressed(){
    MacawApplication.startedApp = FALSE;
    super.onBack pressed();
}
 

}

所以,第一次应用程序启动进入if块,并启动了第一个真正的活动在我们的应用程序。如果通过该应用程序,那么preSS HOME导航,然后重新启动应用程序, DummyActivity 将调用第二次,它会只是调用本身和应用程序完成()将之前显示的最后一个活动pressed HOME。

While developing a sample android application i have constructed two activities 1)Activity 1 2)Activity 2

Now Activity 2 is the foreground activity whereas Activity 1 is the background one. Now user presses Home key. The application(i.e. both the activities) dissappear. Now is we relaunch the application we see Activity 1 as the foreground activity. My question is:

1)Does the platform maintain any history entry when pressed home key? 2)How do we take the user to the last launch activity on relaunching the application?

解决方案

I've struggled with this odd behavior for more than a month but I finally found out the explanation by trial and error.

This behavior happens when you start your application from Eclipse, from command line or if you install an application and press on the Open button(instead of the Done button) to start the application right after you installed it.

If in one of those cases, you start your apllication, go to Activity1 and then to Activity 2, press HOME button and then press the application icon, it will open a new instance of Activity1. Don't take my word for it. Just press BACK and see that it gets you to your Activity2 that you left when you pressed HOME.

It seems that the launcher activity is not put on the activity stack if the application is started in one of those ways mentioned above so that's why it creates a new instance of the launcher activity on top of the current activities in the application's stack. This looks to me like a bug.

So, the workaround would be to exit the application, the first time it was started from Eclipse or command line or Open button etc., by pressing the BACK button as many times as needed, and then enter the application again. From then on, the behavior will be as expected.

EDIT: A better workaround would be this: Create a DummyActivity and set it up as the main entry point into the application. Also, add the flag android:noHistory="true". The DummyActivity is simple and would look like this:

public class DummyActivity extends Activity {

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

    if (!MyApplication.startedApp) {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    }

    finish();
} }

MyApplication is a class that extends android.app.Application and is defined inside AndroidManifest.xml. In HomeActivity.class you set inside the onCreate() method the boolean field startedApp to true. If the user presses BACK from the screen, you need to move the value for startedApp to false.

public class HomeActivity extends Activity {

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

    MyApplication.startedApp = true;
}

@Override
public void onBackPressed() {
    MacawApplication.startedApp = false;
    super.onBackPressed();
}

}

So, the first time the app is launched it enters the if block and launches the first real activity in our application. If you navigate through the app, then press HOME and then launch the app again, DummyActivity will get called a second time and it will just call finish() on itself and the app will show the last activity before you pressed HOME.

这篇关于Home键preSS行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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