Bundle params=new Bundle();
params.putBoolean("isNew", true);
getFragmentManager().beginTransaction()
.replace(R.id.main, Fragment
.instantiate(LandingScreen.this, "com.fragments.FragmentOne",params)).commit()
Now this is Fragment1
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
return root;
}
}
Where do i receive the Bundle params, that i sent with when creating this Fragment ?
Kind Regards
You'll receive Bundle in Fragment onCreate(....)
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
boolean isNew=this.getArguments().getBoolean("isNew");
}
You can get the data where Bundle object is accessible as a parameter
onCreate()
onCreateView()
onActivityCreated()
When you use onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
String myData=this.getArguments().getString("myData");
}
When you use onCreateView()
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
String myData=this.getArguments().getString("myData");
return inflater.inflate(R.layout.example_fragment, container, false);
}
When you use onActivityCreated()
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String myData=this.getArguments().getString("myData");
}
Most of the time, you'll use call getArguments() in onCreate(), which gets called sometime after you instantiate the Fragment but before onCreateView() and onActivityCreated() would get called. However, as per Android documentation, if you're calling this from your Activity while it's being created, you're not guaranteed that the Activity has finished initializing before onCreate() is called:
Note that this can be called while the fragment's activity is still in the process of being created. As such, you can not rely on things like the activity's content view hierarchy being initialized at this point. If you want to do work once the activity itself is created, see onActivityCreated(Bundle).
For more information, check out this blog post on Activities and Fragments: http://www.zerotohired.com/2015/02/passing-data-between-activities-and-fragments-in-android.
Related
I have the next fragment:
public class MyFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
ButterKnife.setDebug(true);
ButterKnife.bind(this, view);
}
#OnClick(R.id.button)
public void onButtonClicked () {
//Do some stuff
}
}
OnClick method works like a charm first time and it works fine until I rotate the device. When I rotate the device, method doesn't works more.
Log has no errors.
Do you know what is the problem?
Thanks you.
Try to retain the fragment to avoid to be destroyed and created again.It may resolve you problem
Try unbinding when the fragment is destroyed (which should happen upon rotation). Then it should bind correctly again:
#Override
public void onDestroyView(){
super.onDestroyView();
ButterKnife.unbind(this);
}
What is the best and safer approach for holding an instance of the Activity
private Activity mActivity;
First approach:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
}
Second approach:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.from(getActivity()).inflate(R.layout.fragment_main, container, false);
mActivity = (Activity) view.getContext();
return view;
}
Well, it depends. I would advice you to use getActivity().
You can use it after onAttach(Activity activity). If you look at the FragmentManager source code, you can see there, that the mActivity field of Fragment is set just before calling onAttach.
But you have to be careful, the activity is not fully initialized (views,..) before onActivityCreated gets called.
In most cases, you should not need to catch Activity.
But if you want to, my suggestion is,
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mActivity = this;
}
My app work fine on most device but some rooted device. In fragment onActivityCreated, getActivity keep return null. I need Context class to to set up thing below.
So anyone can help me?
Update
public class BaseProductFragment extends Fragment {
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(layout_id, container,
false);
return rootView;
}
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageCacheParams cacheParams = new ImageCacheParams(getActivity(),
Utils.PRODUCTS_CACHE_DIR);
....
}
getActivity "might" return null if called from within onActivityCreated...especially during a configuration change like orientation change because the activity gets destroyed...move that initialization to onAttach...
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//initialize here
}
onActivityCreated is a called when the parent activity's onCreate is called...but remember that it could be "recreated", destroyed during a config change
I want to save the state of my FragmentPagerAdapter so I can reuse it after the orientation of the screen changed. Unfortunately the method saveState returns always null.
My Adapter consists of an ActionBar with two Fragments.
Here ist my method call
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(FRAGMENT, viewPager.getCurrentItem() + 1);
outState.putParcelable(ADAPTER, fragmentTabAdapter.saveState());
super.onSaveInstanceState(outState);
}
But the Adapter in outState is always null. I don't understand why. Is there something special I missed about the use of saveState?
Hope you can help me!
this example works if the fragment is not destroyed
private int pageID;
#Override
public void onPause() {
super.onPause();
pageID = mViewPager.getCurrentItem(); // 1 save
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mViewPager.setCurrentItem(pageID, false); // 2 load
}
I have used multiple tabs in fragment class. I was failed in saving the state of fragment. I used the method onSaveInstanceState() and save a parameter in it. But when I came back to this fragment the saved state always shows "null". I tried all the questions and answers regarding to this.Still I am facing the same problem. Please could anyone help me to solve this.
MY CODE
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
super.onCreate(savedInstanceState);
Log.d("savedInstanceStateInfo1", "" + savedInstanceState);
if (null != savedInstanceState) {
nAndroids = savedInstanceState.getInt("nAndroids");
Log.d("NANDROIDS", "" + nAndroids);
}
}
// Log.d("State in oncreate : ",""+savedInstanceState);
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
Log.d("SavedinstanestateInfo", "" + savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null) {
return null;
}
// final ActionBar ab=getActivity().getSupportActionBar();
View v = (LinearLayout) inflater.inflate(R.layout.infotab, container, false);
Log.d("SavedinstanestateInfo", "" + savedInstanceState);
return v;
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("nAndroids", nAndroids);
super.onSaveInstanceState(outState);
}
1) Apparently when using setRetainInstance(true), the Bundle for onRestoreInstanceState is null.
https://stackoverflow.com/a/8488107/1011746
2) Not necessary, but more customary to call super.onSaveInstanceState(outState) before making any modifications to outState. Same for super.onRestoreInstanceState(outState) in onRestoreInstanceState(Bundle outState).