重振来自同一国家我的应用程序被失败了,哎呀:( [英] Relaunching my app from same state gets failed, Oops :(

查看:166
本文介绍了重振来自同一国家我的应用程序被失败了,哎呀:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在这里说的是我现在面临从它被离开之前相同的状态重新启动我的应用程序的问题。我已经做了很多的研究及发展; D于这一点,并通过贴在stackoverflow.So问题消失取悦没有必要地说,它是一个重复的

What I am trying to say here is the problem I'm facing to relaunch my app from same state where it was left before. I have did lot of R&D on this and have gone through the questions posted in stackoverflow.So please it is not necessary to say that it is a duplicate.

我试图把这些选项
   objIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
 同时开始的活性。我也曾经尝试这样做,

I tried putting these options
objIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
while starting the activity. Also I have tried this,

objIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 

最后,我试过这个方法还

And finally I tried this method also

    objIntent.setAction(Intent.ACTION_MAIN);
    objIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

但这些似乎是为我工作。

But none of these seem to be working for me.

该方案是这样的,在那里失败。

The scenario goes like this, where this fails.

  1. 我跑我的应用程序,请从最初的活动有一定的活动说,从A到B

  1. I run my application, go to some activity from the initial activity say from A to B

现在我将preSS我的手机Home键,并做一些操作,如点击照片等。

Now I will press my phone home button and do some actions like clicking photo and etc.

我又尝试推出从主屏幕上的应用程序。

Again I try to launch the app from home screen.

哎呀!现在,我的应用程序没有保留它的状态,它是从初始状态启动。

Oops!! Now my app has not retained its state, it is launching from the initial state.

注意:

  1. 然而,这不是在所有情况下如此。有时候说的话有1〜2次出10或15倍,从它被离开之前相同的状态启动。

  1. However this is not true in all the cases. some times say around 1 or 2 times out of 10 or 15 times, it launches from the same state where it was left before.

还有一个疑问,我已经是我需要设置此为所有的意图?说我有意向,即一些10+的位置,开始活动。我是否需要添加此片code在所有这些地方?我包括这2或3案件

One more doubt i have is do I need to set this for all the intents? Say i have some 10+ positions of Intent i.e., starting the activity. Do I need to add this piece of code in all those places? I included this for 2 or 3 cases

更新:

这个工程在调试模式,但是当涉及到​​一个APK创造的角度来看,它是越来越失败。但是,如果我尝试的次数它给了我所需要的结果,后行为的方式符合预期。打破我的脑袋理解,为什么它的发生是这样的。

任何人知道答案了吗?

推荐答案

是,终于有可能!! 由于谷歌论坛。我提到这 https://github.com/cleverua/android_startup_activity

Yes, finally it is possible!! Thanks to google forum. I referred this https://github.com/cleverua/android_startup_activity.

我就照这是,创建一个启动活动。它里面的onCreate方法,检查()称为needStartApp一个自定义的方法或者一些与你的约定。在此得到了正在运行的任务信息,并将它与我们的应用程序包名称进行比较。如果这是真的,开始你的初始活性。其余一切顺利。

What I did according to this is, create a startup activity. Inside it's oncreate method, check for a custom method called needStartApp() or something with your convention. In this get the running tasks info and compare it with our application package name. And if it is true, start your initial activity. Rest everything goes fine.

下面是额外的活动,您应该添加,

Here is the extra activity you should add,

public class StartupActivity extends Activity {

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

    if (needStartApp()) {
        Intent i = new Intent(StartupActivity.this, MainActivity.class);
        startActivity(i);
    }

    finish();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // this prevents StartupActivity recreation on Configuration changes 
    // (device orientation changes or hardware keyboard open/close).
    // just do nothing on these changes:
    super.onConfigurationChanged(null);
}

private boolean needStartApp() {
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);

    if (!tasksInfo.isEmpty()) {
        final String ourAppPackageName = getPackageName();
        RunningTaskInfo taskInfo;
        final int size = tasksInfo.size();
        for (int i = 0; i < size; i++) {
            taskInfo = tasksInfo.get(i);
            if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
                // continue application start only if there is the only Activity in the task
                // (BTW in this case this is the StartupActivity)
                return taskInfo.numActivities == 1;
            }
        }
    } 

    return true;
   }
 }

最后,不要忘了这个权限添加到清单

At the end, do not forget to add this permission to the manifest

    <uses-permission android:name="android.permission.GET_TASKS" />

的Andr​​oid操作系统是非常好的,但它是不完美的。

这篇关于重振来自同一国家我的应用程序被失败了,哎呀:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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