jameshwart lopez
jameshwart lopez

Reputation: 3131

MainActivity to Fragment and from that Fragment back to MainActivity

Solve by using getActivity()
I have this MainActivity.java and RepeatEntry.java

inside my MainActivity i have this code to have RepeatEntry ui

//i did hide two linear layout here with buttons and edittext inside it ,using the following method 
 hideTwoLinearLayout();
 showCategoryContainerLayout();
Fragment fragment = new RepeatEntry();
 FragmentManager fm = getFragmentManager();
 FragmentTransaction ft = fm.beginTransaction();
  //category_cont is a linear layout container for my fragment
 ft.replace(R.id.category_cont, fragment);
 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 ft.commit();

inside my RepeatEntry.java sample code

Button k = (Button) v.findViewById(R.id.button);
        k.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Intent intent = new Intent(getActivity(),MainActivity.class);
               // startActivity(intent);

               // if i use popBackStack and also remove the code for intent , i cannot show what i hide  
//note i have a method inside mainactivity to showTwoLinearLayout()
               getFragmentManager().popBackStack();
            }
        });

Now my question is, do i have other option other than using intent to go back to MainActivity view

Note:Edited

Upvotes: 1

Views: 5592

Answers (4)

jameshwart lopez
jameshwart lopez

Reputation: 3131

any way i solve my problem just in case if anyone might see this.I came up with two solution which i think the first one is best for me.Thanks to Adnan Basar who give me idea.

First solution is so simple by just adding this code inside my onClick event

((MainActivity) getActivity()).showTwoLinearLayout();
((MainActivity) getActivity()).hideCategoryContainerLayout();

Second Solution I created Activity after extends Fragment{

Activity mainActivity;

And use override methods

 @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity= activity;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // this is where i call the method for showing the twolinearlayout again  
          ((MainActivity) mainActivity).showTwoLinearLayout();
          ((MainActivity) mainActivity).hideCategoryContainerLayout();
    }

Upvotes: 0

adnbsr
adnbsr

Reputation: 635

There is a method called onAttach in your fragment. You can write an interface and assign your fragment's activity like:

private MyFragmentListener mListener;


@Override public void onAttach(Activity activity) {
    super.onAttach(activity);
    mListener = ((MainActivity) activity);
}

public interface MyFragmentListener{ 
  void onClicked(int value);
}

//Call your listener when button clicked or other events
... mListener.onClicked(position);

Or another solution is to use Otto or Eventbus to get rid unnecessary code

Upvotes: 1

Ankitkumar Makwana
Ankitkumar Makwana

Reputation: 3485

You can also go back in Fragment like

getActivity().getSupportFragmentManager().popBackStack();

Upvotes: 0

Gopal Singh Sirvi
Gopal Singh Sirvi

Reputation: 4649

You can add the transaction to backstack and then reverse with poping the backstack the code is here

 Fragment fragment = new RepeatEntry();
 FragmentManager fm = getFragmentManager();
 FragmentTransaction ft = fm.beginTransaction();
  //category_cont is a linear layout container for my fragment
 ft.replace(R.id.category_cont, fragment).addToBackStack("tag");
 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 ft.commit();

And for going back to the activity view call this to pop backstack

FragmentManager fm = getFragmentManager();
fm.popBackStack();

Also you can use the tag to poping back the specific transaction with

fm.popBackStack("tag");

Upvotes: 2

Related Questions