首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何立即显示密码活动,而不使现有活动从背景中闪烁几秒钟?

如何立即显示密码活动,而不使现有活动从背景中闪烁几秒钟?
EN

Stack Overflow用户
提问于 2019-08-16 06:19:35
回答 1查看 124关注 0票数 0

我想添加密码屏幕,始终在堆栈顶部时,应用程序打开背景。我能够找到方法,当应用程序来自背景使用ActivityLifecycleCallbacksLifecycleObserver,并可以显示密码屏幕在onStart的基本活动。但在几秒钟内,用户会从后台查看现有活动,然后尝试使用密码活动。我想要一种方式,以便我可以添加密码活动时,应用程序将背景。但是使用这些方法,我只能知道应用程序什么时候已经进入后台了。我不可能在不启动密码的情况下将密码活动添加到堆栈中。

EN

回答 1

Stack Overflow用户

发布于 2019-08-16 08:06:39

您可以在BaseActivity: Activity中使用片段:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>`
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57519744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档