Я создал приложение, когда тестирую мое приложение, или я нажал кнопку «Назад», мой фрагмент перекрывает другой фрагмент. Я не знаю, почему это происходит. Пожалуйста, помогите мне, мне нужна помощь .. Мои кодыФрагмент перекрывается
public class MainActivity extends AppCompatActivity {
private SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
initFragment();
}
private void initFragment(){
Fragment fragment;
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
fragment = new ProfileFragment();
}else {
fragment = new LoginFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,fragment);
ft.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
android:background="#ff51b46d"
tools:context="com.example.hldev.docup.MainActivity">
<LinearLayout 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="com.example.hldev.docup.MainActivity">
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Uploads.java
public class Uploads extends Fragment implements View.OnClickListener{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_uploads, container, false);
ivAttachment = (ImageView)view.findViewById(R.id.ivAttachment);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_email = (TextView)view.findViewById(R.id.tv_email);
bUpload = (Button)view.findViewById(R.id.b_upload);
tvFileName = (TextView)view.findViewById(R.id.tv_file_name);
ivAttachment.setOnClickListener(this);
bUpload.setOnClickListener(this);
return view;
}
private void goToProfile(){
Fragment profile = new ProfileFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,profile);
ft.commit();
}
}
вместо замены попытки добавления. – Shekhar
Я создал 3 фрагмента, все фрагменты перекрываются, я вызываю фрагмент его функцией private void goToProfile() { Профиль фрагмента = новый ПрофильFragment(); FragmentTransaction ft = getFragmentManager(). BeginTransaction(); ft.replace (R.id.fragment_frame, профиль); ft.commit(); }, но фрагмент перекрывается. – Misty
Сделайте одну вещь, придайте фону как белый от каждого фрагмента, затем попробуйте. – Shekhar