我想添加密码屏幕,始终在堆栈顶部时,应用程序打开背景。我能够找到方法,当应用程序来自背景使用ActivityLifecycleCallbacks或LifecycleObserver,并可以显示密码屏幕在onStart的基本活动。但在几秒钟内,用户会从后台查看现有活动,然后尝试使用密码活动。我想要一种方式,以便我可以添加密码活动时,应用程序将背景。但是使用这些方法,我只能知道应用程序什么时候已经进入后台了。我不可能在不启动密码的情况下将密码活动添加到堆栈中。
发布于 2019-08-16 08:06:39
您可以在BaseActivity: Activity中使用片段:
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ifConditionTrue) {
//Launch whatever fragment you want
} else {
addFragment(new PasswordFragment());
}
}
// Replace current Fragment with the destination Fragment.
public void addFragment(Fragment destFragment) {
// First get FragmentManager object.
FragmentManager fragmentManager = getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Replace the layout holder with the required Fragment object.
fragmentTransaction.replace(R.id.fragment_container, destFragment);
fragmentTransaction.addToBackStack(null);
// Commit the Fragment replace action.
fragmentTransaction.commit();
}}base_activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top" />
</androidx.constraintlayout.widget.ConstraintLayout>`https://stackoverflow.com/questions/57519744
复制相似问题