androidandroid-fragmentactivity

My intent is to start an activity with a specific fragment


I'm trying to figure out how to start a different activity with a specific fragment. This is not working and it seems to me that the application restarts.

Intent i = new Intent(DetailOrderActivity.this, MainActivity.class);
HistoryFragment fragment = new HistoryFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.Container, fragment);
transaction.commit();
startActivity(i);
finish();

Solution

  • In DetailOrderActivity.Java remove fragment code from Intent

    Intent i = new Intent(DetailOrderActivity.this, MainActivity.class);
        i.putExtra("fragment", 2);
        startActivity(i);
        finish();
    

    In MainActivity.Java you will write this code

     Intent intent=getIntent();
            int idValue= intent.getIntExtra("fragment", 0);
            if (idValue==2){
                HistoryFragment fragment = new HistoryFragment();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.Container, fragment);
                transaction.commit();
            }