0

There is an Activity whith FrameLayout.

I need:

1.add(replace) fragment A in Activity

2.add picture in ImageView in fragmet A (an example by button onclick)

3.replace in container fragment A to fragment B

4.if i press button Back on device, I will see fragment A without Image (and other changes )

I used this code to replace fragments

 FragmentManager fragmentManager = getSupportFragmentManager ();
 FragmentTransaction transaction = fragmentManager.beginTransaction ();
 transaction.replace (R.id.container, fragmentB, tag) .addToBackStack (null) .commit ();

In Activity there is a code:

 @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            FragmentManager manager = getSupportFragmentManager();
            int count = manager.getBackStackEntryCount();

            if(count==0) {
                super.onBackPressed();
            }else{
                manager.popBackStack();
            }
        }
    }

If you do the same things with two activities-there are no problems. Image exists on screen after button Back was pressed.

what did I do wrong?

chim
  • 3
  • 1

2 Answers2

0

Well, just put this inside onCreate() //Host Activity

fragment = new fragmentA();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.framelayout, fragment);

        fragmentTransaction.commit();

//now hen you back press, You can see your image ! which is missing

hemen
  • 1,460
  • 2
  • 16
  • 35
0

Try below code:

First create Global object of View class, then define view like below:

private View view;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // Check view already created or not 
        if(view==null)    
          view = inflater.inflate(R.layout.article_view, container, false);

        return view; 
    }
nivesh shastri
  • 430
  • 2
  • 13