过渡后,第一个片段仍保留在背景中 [英] First fragment remains in the background after transition

查看:108
本文介绍了过渡后,第一个片段仍保留在背景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个具有启动屏幕,登录屏幕和注册屏幕的应用程序.

I am currently trying to create an app having a splash screen, a login screen and a register screen.

实现我真正需要的唯一方法是通过片段.尽管当我尝试从初始屏幕过渡到登录片段时,仍然保留了初始屏幕中的文本视图.从登录片段过渡到寄存器片段以及从寄存器片段过渡到登录片段后,也会发生相同的情况.重要的是要注意,在登录和注册之间或注册和登录之间的过渡期间没有任何问题-除了编辑文本停留在其中的事实.

The only way to achieve exactly what I need is through fragments. Though when I try to transition from the splash screen to the login fragment the text view which was part of the splash screen remains. The same happens after transitioning from the login fragment to the register fragment as well as from the register fragment back to the login fragment. It is important to note that there is no issue during transitioning between login and register or between register and login - except the fact that the edit text stays there.

我的 MainActivity.java

public class StartActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
    }

    @Override
    public void onBackPressed(){
        Toast.makeText(getApplicationContext(), "Back button is disabled", Toast.LENGTH_SHORT).show();
    }
}

我的 MainActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".StartActivity"
    android:background="@mipmap/background">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpagerstart"
        android:layout_centerInParent="true"
        android:name="com.example.distributedsystemsui.SplashFragment">

    </fragment>
</RelativeLayout>

SplashScreen.java

public class SplashFragment extends Fragment {
    LinearLayout linearLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_splash, container, false);
        linearLayout = (LinearLayout) view.findViewById(R.id.f_linearsplash);
        Animation animation_fadein = AnimationUtils.loadAnimation((StartActivity)getActivity(), R.anim.fade_in);
        linearLayout.startAnimation(animation_fadein);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.slide_enter_left, R.anim.slide_exit_left,
                        R.anim.slide_enter_right, R.anim.slide_exit_right);
                LoginFragment loginFragment = LoginFragment.newInstance();
                ft.replace(R.id.viewpagerstart, loginFragment);
                ft.commit();
            }
        }, 3000);
        return view;

    }
}

以及 SplashScreen 片段的布局.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginFragment"
    android:background="@color/Tranparent"
    android:id="@+id/frame_splash">

    <LinearLayout
        android:id="@+id/f_linearsplash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/f_splashlogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="70dp"
            android:background="#FFFFFF"/>

        <TextView
            android:id="@+id/f_splashtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="100dp"
            android:background="#FFFFFF"/>
    </LinearLayout>
</FrameLayout>

我所有的搜索都没有结果.

All my search led back to nothing.

我只能假设MainActivity.xml中包含的以下内容造成了混乱(我尝试进行了更改,但没有运气):

I can only assume that either the below contained in the MainActivity.xml creates a confusion (which I tried to change but no luck):

android:name="com.example.distributedsystemsui.SplashFragment"

或者我在 SplashScreen.java 中犯了一个我找不到的错误,即使我尝试了5个小时以上才能使其正常工作.

or that I am doing a mistake in the SplashScreen.java that I cannot locate, even though I tried for more than 5 hours to make it work.

推荐答案

问题是您无法替换通过< fragment> 标记添加的片段.

The problem is that you cannot replace fragments added via the <fragment> tag.

相反,您应该:

1)切换到 FragmentContainerView ,它已添加到

1) Switch to FragmentContainerView, which was added in Fragment 1.2.0. It does not suffer from the same issue as the <fragment> tag and lets you do replace operations without issues:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".StartActivity"
    android:background="@mipmap/background">

    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpagerstart"
        android:layout_centerInParent="true"
        android:name="com.example.distributedsystemsui.SplashFragment">

    </androidx.fragment.app.FragmentContainerView>
</RelativeLayout>

2)在布局中使用 FrameLayout 并在活动中手动添加 SplashFragment (这实际上是 FragmentContainerView 为您执行的操作):

2) Use a FrameLayout in your layout and manually add the SplashFragment in your activity (this is essentially what FragmentContainerView does for you):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".StartActivity"
    android:background="@mipmap/background">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpagerstart"
        android:layout_centerInParent="true">

    </FrameLayout>
</RelativeLayout>

这意味着您需要手动添加SplashFragment:

which means you need to add the SplashFragment manually:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
            .add(R.id.viewpagerstart, new SplashFragment())
            .commit()
    }
}

这篇关于过渡后,第一个片段仍保留在背景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆