First fragment remains in the background after transition

Svestis :

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.

My 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();
    }
}

My layout for 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>

The 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;

    }
}

And the layout for the SplashScreen fragment.

<?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.

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"

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.

ianhanniballake :

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

Instead, you should either:

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) 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>

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()
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344948&siteId=1