First Fragment Remains In The Background After Transition
Solution 1:
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"?><RelativeLayoutxmlns: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.FragmentContainerViewandroid: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"?><RelativeLayoutxmlns: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"><FrameLayoutandroid: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()
}
}
Solution 2:
I think it is better to do the fragment transactions from your activity instead of doing it from your fragment. You might consider having functions in your MainActivity
as follows.
publicclassStartActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
publicvoidgoToLoginFragment() {
FragmentTransactionft= getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_enter_left, R.anim.slide_exit_left,
R.anim.slide_enter_right, R.anim.slide_exit_right);
LoginFragmentloginFragment= LoginFragment.newInstance();
ft.replace(R.id.viewpagerstart, loginFragment);
ft.commit();
}
publicvoidgoToRegisterFragment() {
FragmentTransactionft= getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_enter_left, R.anim.slide_exit_left,
R.anim.slide_enter_right, R.anim.slide_exit_right);
LoginFragmentregisterFragment= RegisterFragment.newInstance();
ft.replace(R.id.viewpagerstart, registerFragment);
ft.commit();
}
@OverridepublicvoidonBackPressed(){
Toast.makeText(getApplicationContext(), "Back button is disabled", Toast.LENGTH_SHORT).show();
}
}
And then from your SplashFragment
call the function that is defined in your activity.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
publicvoidrun() {
((MainActivity) getActivity()).goToLoginFragment();
}
}, 3000);
I hope that solves the problem.
Post a Comment for "First Fragment Remains In The Background After Transition"