【问题标题】:Activity open twice活动开启两次
【发布时间】:2018-10-12 05:40:43
【问题描述】:

在我的应用程序中有 3 个页面

1-飞溅

2-登录

3-OTP页面

在启动页面中,使用 putextra 方法将相同的值发送到登录页面 但是登录页面打开了两次,所以当为System.exit(0)添加返回按钮时,登录页面将再次打开, 所以我在清单中添加android:launchMode="singleTask",添加此行传输值后我应该怎么做..有没有其他方法可以防止重复创建活动

活动开始代码

@Override
protected void onResume() {
    super.onResume();
    // The first time mTimeBeforeDelay will be 0.
    long gapTime = System.currentTimeMillis() - mTimeBeforeDelay;
    if (gapTime > SPLASH_SCREEN_MS) {
        gapTime = SPLASH_SCREEN_MS;
    }
    mSplashHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (sharedPreferences.getBoolean("first_time", true)) {
                if (isConnectingToInternet()) {
                    frstrequest();
                } else {
                    ViewDialog1 alert = new ViewDialog1();
                    alert.showDialog(Splash.this, "Make Sure Internet is Connected", R.drawable.no_internet);
                }


            } else {

                Intent i = new Intent(getApplicationContext(), ApplockActivity.class);
                startActivity(i);
                overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
                Splash.this.finish();


            }
        }
    }, gapTime);
    // Save the time before the delay.
    mTimeBeforeDelay = System.currentTimeMillis();
}

@Override
protected void onPause() {
    super.onPause();
    mSplashHandler.removeCallbacksAndMessages(null);
}

清单

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Splash"


            android:screenOrientation="portrait">



        </activity>
        <activity
            android:name=".LoginActivity"
           android:noHistory="true"
            android:screenOrientation="portrait">
        </activity>
        <activity

【问题讨论】:

  • 你的代码在哪里?
  • 这是不同的事情,android:launchMode="singleTask" 首先你应该把你的代码放在你如何开始你的活动的问题中。
  • 贴出你的代码,让大家看看。
  • 分享你的清单文件

标签: android android-activity stack


【解决方案1】:

如果您的活动从任何地方调用两次,那么它将创建两个实例,结果您的活动将打开两次,您可以通过启动模式停止它,请访问this post

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • @Kos 我觉得很好。
  • @EJoshuaS 这个答案看起来更像是对 OP 帖子的评论
猜你喜欢
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
相关资源
最近更新 更多