0

I have a Main activity and I am launching a fragment from it. However when fragment is launched it also shows the layout of the main activity.

what is the best way to stop it? I don't need to save its state it can be relaunched once fragment is closed.

This is how am launching it

FragmentManager fragmentManager = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment);
            fragmentTransaction.addToBackStack("");
            fragmentTransaction.commit();
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • for onbackpressed in fragment , possible duplicate http://stackoverflow.com/q/5448653/1602333 – MohK Jan 06 '16 at 04:38
  • Code? Please share it here – therealprashant Jan 06 '16 at 04:38
  • I have updated the question bro – Atiq Jan 06 '16 at 04:42
  • 1
    Try to add background color of fragment layout – Mohit Suthar Jan 06 '16 at 04:44
  • @MohitSuthar Thanks Man it worked fine :) – Atiq Jan 06 '16 at 04:51
  • @MikeM. I do need 2 fragments in this activity, as project requirment so i was thinking if it won't work well then I will remove UI of main activity and add another fragment instead. Changing the background works for now is there any problem with that? – Atiq Jan 06 '16 at 04:53
  • Well, it's not the greatest solution, as you're essentially just hiding UI elements that are still there with the background color. It might cause problems, but we'd have to know more about your design to recommend anything. If I understand what you mean by "remove UI of main activity and add another fragment instead", then that sounds like it might be the better option. (Sorry, I accidentally deleted my previous comment when I was trying to edit.) – Mike M. Jan 06 '16 at 04:56
  • Actually this a book app, main menu contains chapter numbers, and when user taps a fragment is loaded and it contains exercises numbers in scroll view and when user taps on an exercise 3rd fragment is loaded which contain scanned image of exercise. So now am thinking i will remove main menu UI and add another fragment there and then swap fragments. Right now I have added a color to fragment layout though and it looks fine :) – Atiq Jan 06 '16 at 05:10

2 Answers2

0

The best way to handle this kind of situation is to use two fragments (FirstFragment will contain the Ui of ParentActivity) and let the ParentAcivity contain just the FrameLayout as container. And then you can keep replacing fragments in that container without any problem.

Atiq
  • 14,435
  • 6
  • 54
  • 69
0

1.put

findViewById(R.id.xxx).setVisibility(View.GONE); 

before getSupportFragmentManager... to hide the view on activity that you want to hide

2.override onBackPressed() to come back from fragment to activity

@override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0 ) {
        findViewById(R.id.xxx).setVisibility(View.VISIBLE);
        getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
    else {
        super.onBackPressed();
    }

}

iwantacat
  • 1
  • 1