Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

android - Activity And Fragment Interaction

I have an Activity with multiple Fragments. I want to show a DialogFragment or open another Fragment from one of the Fragments. I know that an Activity should be the one tasked with opening Fragments so instead I have tried a couple things.

FIRST
I tried to use getActivity() and cast it so I can call a method in the Activity to show a Fragment however this creates a dependency in the Fragment with the Activity and I would like to avoid adding a dependency if possible.

SECOND
Next I tried a listener to notify the Activity that it should show a Fragment. So I created a class in the Activity to implement the listener interface. But I had problems because I had to use New MyActivity().new Listener(); and it would throw an Exception when I tried to use getSupportFragmentManager() since this instance of the Activity is not initialized.

THIRD
I then tried to have the Activity implement the listener directly which works because then I am only creating a dependency with the listener and not the Activity. However now I am getting to the point where my Activity will be implementing 2 - 4 different interfaces which is making me hesitant because it will severely reduce cohesion.

So any way I have tried I seem to be running into a brick wall and creating dependancies I'm not sure I need to be creating. Am I screwed and have to go with one of these options? If so which option would be best? Any help or suggestions are are greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Create interface

public interface ListenFromActivity {
    void doSomethingInFragment();
}

In Activity class keep refrence of ListenFromActivity interface

 public ListenFromActivity activityListener;   

Make public method to set listener

 public void setActivityListener(ListenFromActivity activityListener) {
        this.activityListener = activityListener;
    }

Add some trigger point in activity class, here I have used user interaction

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();

        if (null != activityListener) {
            activityListener.doSomethingInFragment();
        }
    }

Now in Fragment class

make your fragment implement interface class

public class SomeFragment extends Fragment implements ListenFromActivity

Android studio will prompt you to implement method of interface in fragment

 void doSomethingInFragment()
{//Add your code here 
}

Final part part listener instance to activity like this in fragment onCreate method

((ListnerActivity) getActivity()).setActivityListener(SomeFragment.this);

DONE!!. now you can call fragment method from activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...