-1

I use the one frame on which changing the fragment views, the problem I have is when I place add instead of replace the new view is placed on the existing one

all I need is it has to replace the existing and place the new one

As I have to reuse the current view when back button is pressed cannot use the replace

Fragment fragment = null;
            fragment = new DetailFragment2();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .add(R.id.frame_container2, fragment)
                    .addToBackStack(null).commit();

2 Answers2

1

If you don't use the support library (which is your actual case), use this code:

final FragmentTransaction ft =
            getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();

If one day you'll decide to use the support library, then use this instead (it's only a matter of replacing getFragmentManager() with getSupportFragmentManager()):

final FragmentTransaction ft =
            getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    all i need is replace functionality with add it may sounds odd but my req is i has a list view which is downloaded from rest call and when i click on a item it is calling the detail activity and when i press back it has to return to the old view – user3060030 Mar 18 '15 at 08:57
  • My answer is a solution for `hide existing fragment and load new one on same frame` and `all I need is it has to replace the existing and place the new one`. So, in `onCreate()` load the first Fragment. when you click some Button or whatever, replace it with se second one. in `onBackPressed()`, reload the Activity (which, in turn, will reload the first Fragment) - or simply replace the second Fragment with the first one. – Phantômaxx Mar 18 '15 at 09:00
  • @KlingKlang And how exactly can we "simply replace the second Fragment with the first one" if it's been removed and recreation launches a network request which might return different results this time? – kaay Oct 12 '18 at 15:09
  • @kaay You should have a method which allows you to get the same results based on the same conditions (a query or whatever). – Phantômaxx Oct 12 '18 at 15:21
  • @KlingKlang That sounds like really, really bad advice. The request gets the most recent posts, or somesuch, and the service might not be mine to modify to add a functionality like this - not to mention... can you imagine trying to JUSTIFY such a change? I've tried the instancestate bundle, but, after an hour of boilerplating, bound out the mechanism isn't even used in Fragment replacement. – kaay Oct 23 '18 at 06:07
0
FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction;
            transaction = fragmentManager.beginTransaction();
           transaction.replace(R.id.frame_container2,fragment.newIstance());
            transaction.addToBackStack(null);
            transaction.commit();

And you need to create newIstance in Fragment class:

public static fragment newIstance(){
        fragment frag= new fragment();
        return frag;
    }
Slaiv206
  • 309
  • 2
  • 14