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

Categories

I have recently just learnt how to use a fragment manager to send data between two fragments as shown in the codes below:

My First fragment that I am sending the data from:

livefeed_fragment fragment = livefeed.newInstance(InputValue1, InputValue2, InputValue3, InputValue4);
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, fragment).commit(); 

Second Fragment that is receiving the data:

public static livefeed_fragment newInstance(String InputValue1, String InputValue2, String InputValue3, String InputValue4) {
        livefeed_fragment fragment = new livefeed_fragment();
        Bundle args = new Bundle();
        args.putInt("Value1", Integer.parseInt(InputValue1));
        args.putInt("Value2", Integer.parseInt(InputValue2));
        args.putInt("Value3", Integer.parseInt(InputValue3));
        args.putInt("Value4", Integer.parseInt(InputValue4));
        fragment.setArguments(args);
        return fragment;
}

public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView value1 = view.findViewById(R.id.Value1);

        if ( getArguments() != null) {
            Value1 = getArguments().getInt("Value1");
        }

        value1.setText(String.valueOf(Value1));
}

My problem lies with the fact that the first fragment is constantly replacing itself in order to update the new sets of data that come in. Hence, due to this constant replacement of the fragment, if I were choose to navigate to a new fragment, say "Fragment 3", the app crashes. I want to physically stop this replace() command, is there a way to do that?


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

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...