Android 未完成将活动发送到后台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2041891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:19:42  来源:igfitidea点击:

Sending Activity to background without finishing

androidandroid-activity

提问by 1HaKr

How can I make an activity go to background without calling its finish()method and return to the Parent activity that started this? I tried so much but I could not find a solution. So if you guys could help I would be very thankful.

如何在不调用其finish()方法的情况下使活动进入后台并返回启动此活动的父活动?我尝试了很多,但找不到解决方案。所以如果你们能帮助我,我将非常感激。

回答by mhsmith

The following will work if the Activity is running in a different task to its parent. To achieve this, see http://developer.android.com/guide/topics/manifest/activity-element.html#lmode.

如果 Activity 在与其父级不同的任务中运行,以下将起作用。要实现这一点,请参阅http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

public void onBackPressed () {
    moveTaskToBack (true);
}

The current task will be hidden, but its state will remain untouched, and when it is reactivated it will appear just as it was when you left it.

当前任务将被隐藏,但其状态将保持不变,当它被重新激活时,它将显示为您离开时的状态。

回答by stormin986

If you have data you want to cache / store / process in the background, you can use an AsyncTaskor a Thread.

如果您想在后台缓存/存储/处理数据,则可以使用AsyncTaskThread

When you are ready to cache / transition to parent, you would do something like the following in one of your child Activity methods (assuming you started child with startActivityForResult() )

当您准备好缓存/过渡到父级时,您将在您的子 Activity 方法之一中执行以下操作(假设您使用 startActivityForResult() 启动子项)

Thread Approach:

线程方法:

Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            // PUT YOUR CACHING CODE HERE

        }
});

t1.start();

setResult( whatever );
finish();

You can also use a Handlerif you need to communicate anything back from your new thread.

如果您需要从新线程返回任何内容,您也可以使用Handler

AsyncTask Approach:

异步任务方法:

new CacheDataTask().execute( data params );
set_result( whatever );
finish();

The AsyncTask is for situations in which you need to process something in a new thread, but be able to communicate back with the UI process.

AsyncTask 适用于您需要在新线程中处理某些内容但能够与 UI 进程进行通信的情况。

回答by Raj008

Simply

简单地

protected void minimizeApp() { Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); }

protected void minimumApp() { Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 开始活动(开始主);}

回答by Ungureanu Liviu

Try:

尝试:

 Intent toNextActivity = new Intent(CurrentActivity.this,
                                     NextActivity.class);
 CurrentActivity.startActivity(toNextActivity);

If you use this way, the method onPause() from CurrentActivity will be called and if you have a static variable (like a MediaPlayer object) in CurrentActivity it will continue to exist (or play if it is playing)..

如果您使用这种方式,则 CurrentActivity 中的 onPause() 方法将被调用,如果您在 CurrentActivity 中有一个静态变量(如 MediaPlayer 对象),它将继续存在(如果正在播放,则播放)。

I'm using that in my application but I found a better way to do that with services.

我在我的应用程序中使用它,但我找到了一种更好的方法来使用服务。

Hope this will help you!

希望能帮到你!

回答by Jonathan

Moving the task backward is NOT going to insure that you will maintain any state, unless you do that manually by responding to the Activitylifecycle events.

将任务向后移动并不能确保您将保持任何状态,除非您通过响应Activity生命周期事件手动执行此操作。

You should just start the second Activityvia Intentusing startActivity(), or, if you need to receive a result back from the second Activity, use startActivityForResult(). This allows you to receive a callback when the user finishes the Activityand returns to your first Activity.

您应该Activity通过Intentusing开始第二个startActivity(),或者,如果您需要从第二个接收结果Activity,请使用startActivityForResult(). 这允许您在用户完成Activity并返回到您的第一个Activity.

Starting an Activityand getting results: http://developer.android.com/reference/android/app/Activity.html#StartingActivities

启动Activity并获得结果:http: //developer.android.com/reference/android/app/Activity.html#StartingActivities