Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Fragment on back button pressed Activity is blank

I have an Activity and many fragments inflated in same FrameLayout

<FrameLayout     android:id="@+id/content_frame"     android:layout_width="match_parent"     android:layout_height="match_parent" /> 

example: mainActivity > any fragment (press back button) > activity is blank.

In onCreate:

layout = (FrameLayout)findViewById(R.id.content_frame); layout.setVisibility(View.GONE); 

When I start a fragment:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content_frame, profileFragment); ft.addToBackStack(null); ft.commit(); layout.setVisibility(View.VISIBLE); 

I suppose I need to make the frameLayout's visibility GONE again on back pressed, but how do I do this?


I tried onBackPressed and set layout.setVisibility(View.GONE); but I cannot go back through fragments, as I go directly to main page.

like image 431
Filip Luchianenco Avatar asked Dec 03 '13 00:12

Filip Luchianenco


People also ask

Can a fragment run without activity?

Android app must have an Activity or FragmentActivity that handles the fragment. Fragment can't be initiated without Activity or FragmentActivity.

Can a fragment contain an activity?

A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input events, and you can add or remove fragments while the containing activity is running. This document describes how to create a fragment and include it in an activity.


2 Answers

If you have more than one fragment been used in the activity or even if you have only one fragment then the first fragment should not have addToBackStack defined. Since this allows back navigation and prior to this fragment the empty activity layout will be displayed.

 // fragmentTransaction.addToBackStack() // dont include this for your first fragment. 

But for the other fragment you need to have this defined otherwise the back will not navigate to earlier screen (fragment) instead the application might shutdown.

like image 147
Sampath Kumar Avatar answered Oct 13 '22 06:10

Sampath Kumar


@Override public void onBackPressed() {     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);     if (drawer.isDrawerOpen(GravityCompat.START)) {         drawer.closeDrawer(GravityCompat.START);     }     else {         int fragments = getSupportFragmentManager().getBackStackEntryCount();         if (fragments == 1) {             finish();         } else if (getFragmentManager().getBackStackEntryCount() > 1) {             getFragmentManager().popBackStack();         } else {             super.onBackPressed();         }     } } 

To add a fragment

 getSupportFragmentManager().beginTransaction()                 .replace(R.id.layout_main, dashboardFragment, getString(R.string.title_dashboard))                 .addToBackStack(getString(R.string.title_dashboard))                 .commit(); 
like image 45
Goodlife Avatar answered Oct 13 '22 06:10

Goodlife