taimoor shaukat taimoor shaukat - 3 months ago 90
Android Question

Programmatically go back to the previous fragment

How can i make

onBackPressed()
go on HomeFragment from every fragment or go to previous fragmant.

Here is the code to set fragment by default Home fragment is open when app starts but on back press app gone close.
need help to solve it out thanks
this is my MainActivityDrawer Class

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
GpsTrackerAlarmReceiver alarm = new GpsTrackerAlarmReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);

mToolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);

drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
alarm.setAlarm(this);

// display the first navigation drawer view on app launch
displayView(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_help) {
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}

return super.onOptionsItemSelected(item);
}
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {

case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new ProfileFragment();
title = getString(R.string.title_profile);
break;
case 2:

fragment = new ContactsFragment();
title = getString(R.string.title_contacts);
break;
case 3:
fragment = new GeoFencFragment();
title = getString(R.string.title_geofence);
break;
case 4:
fragment = new HistoryFragment();
title = getString(R.string.title_history);
break;
case 5:
fragment = new SettingFragment();
title = getString(R.string.title_settimg);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
@Override
public void onResume() {
Log.d(TAG, "onResume");
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}}

Answer

I achieved it by doing following:- 1. Do no add fragment transaction in back stack. 2. On back press check if current displaying fragment is not home fragment then show home fragment other wise super.onBackPressd() to close activity.

 if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
        {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        }
     else if (fragmentManager.findFragmentById(R.id.content_frame_drawer_layout) != homeFragment)
                {
                    fragmentManager.beginTransaction().replace(R.id.content_frame_drawer_layout, homeFragment).commit();

                }
                else
                {
                    super.onBackPressed();
                }