Im developing a small android application, i was using shared preference in order to transfer data between fragments. now i want to use bundle but the problem is the bundle in the second fragment getting null value how do i solve that problem ?
here are some part of code
... some usefull Code in first fragment
args.putLong("favoriteCountry", countryListSpinnerData.get(favoriteCountrySpinner.getSelectedItemPosition()).getId());
args.putInt("favoriteCurrency", currencySpinner.getSelectedItemPosition());
args.putDouble("favoriteBudget", Double.parseDouble(budgetEditText.getText().toString()));
args.putString("additionalInformation", additionalInformationEditText.getText().toString());
MoneyPartnerShipStepTwo moneyPartnerShipStepTwo = new MoneyPartnerShipStepTwo();
moneyPartnerShipStepTwo.setArguments(args);
FragmentHelper.NAVIGATE_FRAGMENT(new MoneyPartnerShipStepTwo(), getActivity())
Now the second fragment
#Override
public void onCreate(Bundle savedInstanceState) { // savedInstanceState is null why ???
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.bundle = savedInstanceState;
}
setHasOptionsMenu(true);
}
you create new MoneyPartnerShipStepTwo(), not instance with your argument.
so, please change FragmentHelper.NAVIGATE_FRAGMENT(moneyPartnerShipStepTwo, getActivity())
Related
I am trying to get bundle extras from activity 1 to fragment of activity 2 but getargument() always returns as null.
//Using this to get string in fragment
String value = getArguments().getString("abc");
//activity1 code that i used to get the extras
Bundle bundle = new Bundle();
bundle.putString("abc", townextra);
UserFragment myFrag = new UserFragment();
myFrag.setArguments(bundle);
There is a problem with logic of passing data.
Correct way would be: Pass data from Activity1 to Activity2, and then from Activity2 to Fragment.
all fixed. I added a new function on my activity 2 to create fragment. instead of doing it in oncreat. it all works fine, thanks
Your Activity
Bundle bundle = new Bundle();
bundle.putString("params", "Your String data");
// set MyFragment Arguments
MyFragment myObj = new MyFragment();
myObj.setArguments(bundle);
The Fragment.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam = getArguments().getString("params");
}
}
But will be better if you show the code how you pass data between activities and then use my answer.
And please add your fragment code.
How to get the bundle that is given to this method on request (Bundle outState)?
#Override
protected void onSaveInstanceState(***Bundle outState***) {
super.onSaveInstanceState(outState);
// Only if you need to restore open/close state when
// the orientation is changed
if (adapter != null) {
adapter.saveStates(outState);
}
}
in Bundle instance you can pass the data and get it back in onCreate() method. like following
outState.putString("key1", "data1");
outState.putBoolean("key2", "data2");
outState.putInt("key3", "data3");
and in onCreate get it like following
if (savedInstanceState != null){
data_1 = savedInstanceState.getString("keys1");
data_2 = savedInstanceState.getBoolean("keys2");
data_3 = savedInstanceState.getInt("keys3");
}
You can get saved bundle back using below method:
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// get your saved bundles back here
}
Just refer this developer page, you will get clear idea about this
It's the Bundle sent in the OnCreate method in your activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
savedInstanceState.getString("bla");
}
}
Note that you'll have to check for null, because the first time you create your activity it will be null, since there was no previous state.
see https://developer.android.com/guide/components/activities/activity-lifecycle.html#oncreate for more information
Calling code (run in service):
Intent textIntent = new Intent(this, TextActivity.class);
textIntent.putExtra("text_seq", message.xfer.seq);
textIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(textIntent);
Called code (in TextActivity):
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
Log.d(TAG, "" + bundle.getInt("text_seq"))
...
In fact the whole bundle is lost - the code above throws an NPE when calling bundle.getInt().
I'm sure there's something obvious I have missed...
Bundle you are reading is NOT for that purpose. As per docs
void onCreate (Bundle savedInstanceState)
Bundle: If the activity is being re-initialized after previously being
shut down then this Bundle contains the data it most recently supplied
in onSaveInstanceState(Bundle). Note: Otherwise it is null.
If you need to get extras you need to call:
Bundle extras = getIntent().getExtra();
and then you can try to get your values:
int myVal = extras.getInt(key);
Alternatively you can try to use:
int myVal = getIntent().getIntExtra(key, defaultVal);
Have you tried using getIntent().getInt("text_seq") ?
get your bundle like this
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
Bundle intentBundle = getIntent().getExtra();
Log.d(TAG, "" + intentBundle.getExtra(“text_seq"))
}
The bundle you're using is the savedInstanceState you can read more about it here.
What you need to use is this:
Bundle intentBundle = getIntent().getExtra();
Since you added the bundle to Intent extras, so you need to get it from the getIntent().getExtra()
also you can get individual items like this :
getIntent().getIntExtra("text_seq", defaultValToReturn);
I wrote a custom static method to create my fragment. Fragment is a subclass of android.support.v4.app.Fragment class.
Method to create my fragment is bellow.
public static AddItemFragment newInstance(UUID listId, UUID itemId){
AddItemFragment fragment=new AddItemFragment();
Bundle bundle=new Bundle();
bundle.putSerializable(EXTRA_DATA_LIST_ID,listId);
bundle.putSerializable(EXTRA_DATA_ITEM_ID, itemId);
fragment.setArguments(bundle);
return fragment;
}
In my onCreate method, I am attempting to read data from bundle.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListId = (getArguments().getSerializable(EXTRA_DATA_LIST_ID) == null ? null : (UUID) getArguments().getSerializable(EXTRA_DATA_LIST_ID));
mItemId = (getArguments().getSerializable(EXTRA_DATA_ITEM_ID) == null ? null : (UUID) getArguments().getSerializable(EXTRA_DATA_ITEM_ID));
}
Well the problem is that getArguments() method never returns bundle. It always returns NULL. I don't understand why. savedInstanceState is NULL as well.
Silly me was overriding bundle set in the Fragment with Activity's savedInstanceState bundle which at that point is NULL.
DUH...
i have question about two different Bundle object in below methods :
onSaveInstanceState(Bundle outState);
onCreate (Bundle savedInstanceState);
how android system know that bundle object in onCreate method is object that programmer used for save his/her activity states and onCreate method use that Bundle object to get activity state that is killed by system?
is the Bundle object one of the Activity class Members and super.saveInstanceState(outState);
save the Bundle in the Bundle object of Activity and when an activity call onCreate(Bundle ) method this member send to onCreate method?how can i use Bundle in onCreate( ) method?
please help me...
The values you save in the onSaveInstanceState method's bundle will be sent back to you in onCreate. As an example of how this works.
You get a phone call.
Your Activity is stopped and onSaveInstanceState is called. You put a value into this bundle.
Android finishes your activity and destroys that instance because the OS needs memory.
The user returns to your application.
The bundle is recreated from some type of persistent storage that Android maintains on your behalf. Now your onCreate can grab the value you placed in the bundle during onSaveInstanceState
EXAMPLE
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.lldr_activity);
mFilterCheckbox = (CheckBox) findViewById(R.id.checkbox_id);
if(savedInstanceState != null) {
mFilterCheckbox.setChecked(savedInstanceState.getBoolean("FILTER_STATE", false));
}
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putParcelable("FILTER_STATE", mFilterCheckbox.isChecked());
}