0

I have used two fragments in my application to show the list and detail view. When I press the list fragment it should show the detail view fragment and when I press back it should the list fragment. This flow is working fine but the problem is,it loading every time the list fragment(Instead of resuming the fragment it creating the fragment ). Can any body face problem already please help me to solve this.

//Activity class

public class MainActivity extends ActionBarActivity {

    /** Activity Override - Start */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_folio_level);
        System.out.println("Inside the FolioLevelActivity onCreate() method!!!!!!!!");

        Bundle bundle = getIntent().getExtras();
        FirstFragment fragment = new FirstFragment ();
        fragment.setArguments(bundle);
        if(savedInstanceState==null){
           getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment).commit();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.right_to_left_in, R.anim.right_to_left_out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("Inside the FolioLevelActivity onResume() method!!!!!!!!!");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("Inside the FolioLevelActivity onPause() method!!!!!!!!!");
    }

    /** Activity Override - End */

}

//Fragment 1:

public class FirstFragment extends Fragment implements OnClickListener,OnItemClickListener{

    private ListView mListView;

    /** Override method for drawing the fragment user interface. */
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_folio_level,container, false);
        initViews(rootView);

        getActivity().getSupportFragmentManager().beginTransaction()
         .setCustomAnimations(R.anim.right_to_left_in,R.anim.right_to_left_out,R.anim.left_to_right_in,R.anim.left_to_right_out)
         .replace(R.id.fragment_container, new SecondFragment().addToBackStack(null).commit();

        return rootView;
    }

    /** This function for handling all widget click events*/
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
           case R.id.back_button:
            getActivity().onBackPressed();
            break;

          default:
            break;
         }      
    }

    /** Override - start */
    @Override
    public void onResume() {
        super.onResume();
        mListView.setOnItemClickListener(this);

    }

    @Override
    public void onPause() {
        super.onPause();
        mListView.setOnItemClickListener(null);

    }

}

//Fragment 2:

public class SecondFragment extends Fragment implements OnClickListener {

    /** This override method for drawing the user interface. */
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.fragment_scheme_level,container,false );   

        return rootView;
      }


    @Override
    public void onClick(View v) {
         if(v.getId()== R.id.back_button){
           getActivity().getSupportFragmentManager().popBackStack();
         }      
   }

}
Tulsiram Rathod
  • 1,926
  • 2
  • 19
  • 29
Loganathan
  • 1,864
  • 3
  • 12
  • 17

0 Answers0