在Android中我有一些活动,比方说A,B,C.
在A中,我使用此代码打开B:
Intent intent = new Intent(this, B.class);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在B中,我使用此代码打开C:
Intent intent = new Intent(this, C.class);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
当用户点击C中的按钮时,我想返回A并清除后栈(关闭B和C).因此,当用户使用后退按钮B和C将不会显示时,我一直在尝试以下内容:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
但是当我回到活动A时,如果我使用后退按钮,B和C仍然会出现.我怎么能避免这种情况?
在给定的Android活动中,我想在某个时刻为用户启动一个新活动.一旦他们离开第一个活动并到达第二个活动,第一个活动是陈旧的,我想完全删除它,因此无法再从后退按钮访问它.
如何实现这一目标的最佳方法是什么?如何在用户启动新活动后立即杀死或销毁此活动?
当我点击我的个人资料活动中的退出按钮时,我想将用户带到登录页面,他需要使用新的凭据.
因此我使用了这段代码:
Intent intent = new Intent(ProfileActivity.this,
LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在" onButtonClick注销"按钮中.
但问题是,当我点击登录活动上的设备后退按钮时,它会将我带到ProfileActivity.当我按下LoginActivity上的设备后退按钮时,我希望应用程序关闭.
我究竟做错了什么?
我还在LoginActivityandroid:launchMode="singleTop"的清单中添加了
谢谢
用户启动我的应用程序并登录.
选择会话超时为5分钟.
在应用程序上执行某些操作.(全部在前台)
现在用户将Myapp带到后台并启动其他应用程序.
---->倒计时器启动并在5分钟后退出用户
或用户关闭屏幕.
---->倒计时器启动并在5分钟后退出用户
我想要相同的行为,即使应用程序在前台,但用户不会长时间说6-7分钟与应用程序交互.假设屏幕始终处于开启状态.我想检测用户的不活动状态(即使应用程序位于前台,也没有与应用程序进行交互)并启动我的倒数计时器.
我想结合两个意图标志,就像我们在android中所做的那样
Intent intent = new Intent(this, MapsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情,但它对我不起作用
val intent = Intent(context, MapActivity::class.java)
intent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
Run Code Online (Sandbox Code Playgroud) 我的问题与这类问题略有不同.我需要删除或清除我的活动堆栈,然后开始一项新活动.我不认为这是一个clear_top标志问题.我正在解释一个例子:
我的活动流程:
Login > Home > Screen1 > screen2 ....
Run Code Online (Sandbox Code Playgroud)
我完成登录活动或使用no_history标志调用.所以我的活动看起来像这样
Login(finished)> Home [bottom of the stack now] > Screen1 > Screen2[top of the stack]
Run Code Online (Sandbox Code Playgroud)
我需要处理会话错误.如果在任何点发生任何会话错误,我需要返回登录活动.但请记住,我没有堆栈中的登录活动.所以clear_top不起作用.
如果在Screen2中发生任何会话错误,那么我需要清除完整堆栈(screen2,screen1,home),然后开始登录活动.因此,在返回按钮后按下登录活动将关闭我的应用程序.
有没有办法清除活动堆栈?
提前致谢
我曾经认为,当活动A被另一个全屏活动B替换时,onStop()将调用A的回调.
这也反映在文档中:
活动的可见生命周期发生在对onStart()的调用和对onStop()的调用之间.在此期间,用户可以在屏幕上看到活动并与之交互.例如,当一个新活动开始并且这个活动不再可见时,会调用onStop().
然而,现在,我正在观察不同的行为(在Lollipop和Marshmallow上测试).
我开始AuthenticationActivity从HomeActivity和,尽管事实AuthenticationActivity是一个全屏幕活动,HomeActivity不停止.
在清单中声明这些活动:
<activity
android:name=".screens.home.activities.HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".screens.authentication.activities.AuthenticationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:windowSoftInputMode="adjustResize"
android:theme="@style/AppTheme.Transparent"/>
Run Code Online (Sandbox Code Playgroud)
当活动切换时,这是我在logcat中观察到的:
11-28 10:16:31.443 15183-15183/somepackage I/LOG:: LifeCycle : activity (somepackage.screens.home.activities.HomeActivity@3561e8e1) paused
11-28 10:16:31.583 15183-15183/somepackage I/LOG:: LifeCycle : activity (somepackage.screens.authentication.activities.AuthenticationActivity@2bbdb20f) created
11-28 10:16:31.753 15183-15183/somepackage D/Activity: performCreate Call secproduct feature valuefalse
11-28 10:16:31.753 15183-15183/somepackage D/Activity: performCreate Call debug elastic valuetrue
11-28 10:16:31.753 15183-15183/somepackage D/AuthenticationActivity: onStart()
11-28 10:16:31.753 15183-15183/somepackage I/LOG:: LifeCycle : …Run Code Online (Sandbox Code Playgroud)