Fragment - getArguments () returns an empty bundle

Hope someone can help me figure this out:

  • I use a single-task application and many fragments that are replaced in one container, and I test my application on a real device with the "Do not save actions" option enabled

  • When a new fragment is added (using the method FragmentTransaction replace()), I use the method setArguments()to transfer information to the new fragment. It works as expected, and I can get this information using getArguments()inside this snippet. Everything is still fine ...

  • After that, I send my application to the background. I see that all fragments on the stack are destroyed, as expected

  • I bring my application to the forefront and in the method getArguments()I get empty Bundle(not empty, just empty object) instead of the one that was with the data that I used in # 2

According to the Android documentation, the arguments given in setArguments()will be saved when you destroy the fragments and create ... So, my questions:

  • Will the script that I described be included in the “save in fragment destruction and creation”?

  • Can the "Do not save actions" option work with getArguments()/ setArguments()if it is enabled?

  • Is there any way to check the correct creation / destruction of a fragment besides the option "Do not save actions"?

  • ""? onSaveInstanceState(), , .

+4
7

- , . , , .

  • " " , ?

, "Do not keep Activities" , . , . Android . , .

  1. " " getArguments()/setArguments(), ?

. savedInstanceState onCreate null. , . , .

  1. / " "?

FragmentTransaction.

1 - FragmentTransaction, , .

2 - FragmentTransaction, (, , ). , , .

  1. ""? onSaveInstanceState(), , .

, . Android . onAttach ( , , ) .

+1

, , , onCreate. , :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...
    ...
    fragment = SampleFragment.newInstance("sample");
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.container, fragment, "sample_fragment_tag")
            .commit();
    ...
}

, , , . , savedInstanceState - null, , . :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...
    ...
    if (savedInstanceState == null) {
        fragment = SampleFragment.newInstance("sample");
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment, "sample_fragment_tag")
                .commit();
    } else {
        fragment = (SampleFragment) getSupportFragmentManager().findFragmentByTag("sample_fragment_tag");
    }
    ...
}

, , .

+2

. , . , , @Reyansh @Björn. - , - : getArguments() Bundle , . , - , .

@jDur , .

+1

, , keep Activities , Android . onSavedInstanceState, setRetainState (true) sharedPreferences. SharedPreference , SharedPreference. : , , , . , .

0
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/*Created by Foolish_Guy on 4/29/2017.*/                 

public class TestFragment extends Fragment {
private static final String USER_ID = "param1";
private static final String ARG_PARAM2 = "param2";

String userID;
String mParam2;

public static TestFragment newInstance(String param1, String param2) {
    TestFragment fragment = new TestFragment();
    Bundle args = new Bundle();
    Log.e("Data :", String.valueOf(param1));
    args.putString(USER_ID, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        userID = getArguments().getString(USER_ID);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    return view;
}

}

, . ,

TestFragment frag = TestFragment.newInstance("UID", "TEXT");

, Bundle

onCreate (Bundle savedInstance) , Bundle .

0

onSaveInstanceState - , . , , , , , . , , , .

. , , unistall , , , , . ?

0
setRetainInstance(true);

onCreate . get SupportFragmentManager , null, .

Fragment fragment = fragmentManager.findFragmentByTag(tag);
if(fragment==null){
 fragmentManager.beginTransaction().replace(...
}
0

Source: https://habr.com/ru/post/1675564/


All Articles