android - 使用 onStop 进行解绑服务

标签 android android-activity android-lifecycle

我有一个 Activity,我想在不调用 onDestroy() 的情况下关闭它。所以我使用 onStop(); 看起来像

@Override
    public void onStop(){
        super.onStop();
        try{
            unbindService(mServerConn);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

我运行了代码,但 Activity 在屏幕上仍然可见

你能帮我把它藏起来吗? 谢谢!

最佳答案

onStop() 的用法在那种情况下是不正确的。如果您需要关闭 Activity ,请使用 finish()

Intent openStartingPoint = new Intent(this, MainActivity.class);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(openStartingPoint);
finish();

这应该可以正常工作。

关于android - 使用 onStop 进行解绑服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29664887/

相关文章:

android - 除非重新启动,否则滑动选项卡中的 ListView 不会更新

java - 如何在纹理上绘画 [Libgdx]

android - 在 Activity 和 Service 之间交换对象

android - 当具有 "singleTask"launchMode 的 Activity 进入前台时 - Android

android - 如何检测我的 Activity 何时被遮挡?

android - FragmentManager IndexOutOfBoundsException 无法恢复 Activity 崩溃

java - 无法检索当前小区的 CellID 和 LAC

android - 如何在 Android 中以编程方式创建 'Horizontal style' 进度条?

android.app.Application 无法转换为 LifecycleOwner

android - onStart() 到底是做什么的? - 安卓