Reputation: 41
I have created an app when i'm testing my app or i clicked on back button my fragment is overlapping other fragment. I don't know why it's happening. Please help me i need help.. My codes
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();
}
}
Upvotes: 0
Views: 103
Reputation: 441
Just set a background color to your new <fragment />
in XML file or else it will be transparent and show a replaced fragment.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="schemas.android.com/apk/res/android";
xmlns:tools="schemas.android.com/tools";
android:id="@+id/frag24"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green">
<!-- Your Content -->
</RelativeLayout>
</FrameLayout >
Upvotes: 1
Reputation: 6622
replace your code with below.
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/holo_green_dark"
tools:context="com.example.hldev.docup.Uploads">
Upvotes: 0
Reputation: 6622
set white background color of your parent layout of your activity_uploads
. also do same with all layout which you use in Fragment
.
Upvotes: 0