Search code examples
javaandroidandroid-fragmentsback-button

Working on Fragment Backbutton android


I am working on navigation drawer and fragments and currently i am facing a problem from image Consider (Donut) is HomeFragment class. What i would like to achieve is

this

when user navigate Fragment A(Donut) -> Fragment B(Eclair) then again navigates -> Fragment H(kitkat) and then navigates -> Fragment I(Lollipop)

and user working on it. If user press backbutton it may works based on hierarchy as

super.onBackPressed();

As result pressing back results as Fragment I -> Fragment H -> Fragment B -> finally Fragment A.

Now i need to navigate user Directly to Fragment A without going to other Fragment classes.

I have achieved this using

public class MainActivity extends AppCompatActivity {
//initializing 
int check = 0; in MainActivity
....................
...................
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);

.................
................

 drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) view.findViewById(R.id.textView1);
                String aa = tv.getText().toString();
                selectItemFragment(position, aa);
            }
        });

...................
...................
private void selectItemFragment(int position, String aa) {
        Fragment fragment2;
        FragmentManager fragmentManager = getSupportFragmentManager();
        switch (position) {
            case 0:
                fragment2 = new HomeFragment();
                break;
            case 1:
                fragment2 = new GalaxyFragment();
                break;
            case 2:
                fragment2 = new ShareFragment();
                break;
            default:
                fragment2 = new HomeFragment();
                break;
        }
        check = position;
        ffragm = fragment2;
        fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment2).addToBackStack(null).commit();
        drawerList.setItemChecked(position, true);
        setTitle(aa);
        mDrawerLayout.closeDrawer(drawerList);
    }
..........................
............................
@Override
    public void onBackPressed() {
        if (check == 0) {
            finish();
        } else {
            check = 0;
            Fragment fragment2;
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragment2 = new HomeFragment();
            fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment2).addToBackStack(null).commit();
        }
}

Here i m facing problem, consider when user working inside Galaxyfragment class or any other fragment class, he navigates to another fragment class inside, and then he press backbutton it directly navigates to Fragment A.So, here i need to use back stack hierarchy and finally to Fragment Class and finishes the application please see the below code .

public class GalaxyFragment extends Fragment {
    Context cc;
    Button Addbutton;

    public GalaxyFragment() {
        // Required empty public constructor
    }
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.home_fragment, container, false);
        ll1design = (LinearLayout) v.findViewById(R.id.llone);
        Create_list = (Button) v.findViewById(R.id.submit);
    Addbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                display_button.setVisibility(View.VISIBLE);
                ll1design.setVisibility(View.GONE);
                String value = "";
                Bundle bundle = new Bundle();
                bundle.putString("message", "" + value);
                FragmentManager fragmentManager = getFragmentManager();
                Fragment fragment = new ShareFragment();
                fragment.setArguments(bundle);
                fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).addToBackStack(null).commit();
            }
        });
  return v;
    }
}

So here

i need to(Achieve) use backbutton to working correctly as Galaxy2Fragment -> GalaxyFragment -> HomeFragment class.

Can anyone give me suggestions how to work on these Backbutton fragments on these type of cases. Suggestions are welcome. Thanks.


Solution

  • Easiest method be to replace this line

    fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment2).addToBackStack(null).commit();
    

    to

    FragmentTransaction ft =fragmentManager.beginTransaction()
    ft.replace(R.id.main_fragment_container, fragment2)
    if(fragmentManger.findFragmentById(R.id.main_fragment_container) instanceof HomeFragment)
       ft.addToBackStack(null))
    ft.commit();
    

    You don't required to override onBackPressed if you do this.