I need to pass arguments to my fragments but getArguments() alway returns null
public static PersonFragment newInstance(int columnCount, ArrayList<Person> personenListe) {
PersonFragment personFragment = new PersonFragment();
Bundle args = new Bundle();
args.putSerializable("persList",personenListe);
args.putInt(ARG_COLUMN_COUNT, columnCount);
personFragment.setArguments(args);
return new PersonFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) { //getAguments() == null !!
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
mPersonenListe = (ArrayList<Person>) getArguments().getSerializable("persList");
}
}
I'm calling it in MainActivity
openFragment(PersonFragment.newInstance(personenListe.size(), personenListe));
With this method
public void openFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Instead of returning the fragment that you set the arguments to, you returned a brand new fragment.
So change the newInstance to:
public static PersonFragment newInstance(int columnCount, ArrayList<Person> personenListe) {
PersonFragment personFragment = new PersonFragment();
Bundle args = new Bundle();
args.putSerializable("persList",personenListe);
args.putInt(ARG_COLUMN_COUNT, columnCount);
personFragment.setArguments(args);
// return new PersonFragment();
return personFragment ; // <<< change here
}
Related
I am trying to send data from activity to fragment,but in fragment i am getting null instead of my values,following is my code can any one help me with this?
String nofrndthere="nofrnds";
String frndthere="frnds";
if(jsonary.length()==0)
{
// Toast.makeText(MainActivity.this,"Null",Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("nofrndsavailable", nofrndthere);
HomeFragment fragobj = new HomeFragment();
fragobj.setArguments(bundle);
}
else if(jsonary.length()!=0)
{
// Toast.makeText(MainActivity.this,"Not Null",Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("frndsavailable", frndthere);
HomeFragment fragobj = new HomeFragment();
fragobj.setArguments(bundle);
}
HomeFragment
public class HomeFragment extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
private FragmentTabHost tabHost;
private SearchView searchView;
private String strtext;
private String strtextss,strtextfnrdval;
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
Bundle bundle = this.getArguments();
strtext = bundle.getString("user_login_id");
if (getArguments() != null) {
strtextss = bundle.getString("nofrndsavailable");
strtextfnrdval= bundle.getString("frndsavailable");
}
System.out.println("DATASSSSS : " +strtextss+strtextfnrdval);
System.out.println("<<<<<<<<<<<<<< Session ID : " + strtext+strtextss+strtextfnrdval);
Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
if(strtext!=null) {
arg1.putString("user_logid", strtext);
}
tabHost.addTab(tabHost.newTabSpec("one").setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_profile_tabs,"Home")), DiscoverFragment.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
if(strtext!=null) {
arg2.putString("user_logid_sectab", strtext);
}
tabHost.addTab(tabHost.newTabSpec("Sec").
setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_frnds_tab,"Invite")), ShopFragment.class, arg2);
Bundle arg3 = new Bundle();
arg3.putInt("Arg for Frag3", 3);
if(strtext!=null) {
arg3.putString("user_logid_thirdtab", strtext);
}
tabHost.addTab(tabHost.newTabSpec("Third").
setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_wish_tab,"Wish")), Thirdtab.class, arg3);
Bundle arg4 = new Bundle();
arg4.putInt("Arg for Frag4", 4);
if(strtext!=null) {
arg4.putString("user_logid_fourthtab", strtext);
}
if(strtextss == getArguments().getString("nofrndsavailable"))
{
// Toast.makeText(getActivity(), "Null in home", Toast.LENGTH_SHORT).show();
System.out.println("Null in home");
tabHost.addTab(tabHost.newTabSpec("Four").
setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_notification_tab, "Alert")), FourthTabs.class, arg4);
}
else if(strtextfnrdval == getArguments().getString("frndsavailable"))
{
// Toast.makeText(getActivity(), "Not Null in home", Toast.LENGTH_SHORT).show();
System.out.println("NotNull in home");
tabHost.addTab(tabHost.newTabSpec("Four").
setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_fnrdalert, "Alert")), FourthTabs.class, arg4);
}
return tabHost;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
public View getTabIndicator(Context context, int icon,String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.indicatorImageView);
iv.setImageResource(icon);
TextView txt = (TextView) view.findViewById(R.id.txttb);
txt.setText(text);
return view;
}
}
before you open the fragment this is how you can attach the bundle to it. following code should be in your activity to put the bundle in the fragment.
Fragment fragment = new YourFragmentClass();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
now that you have attached the bundle open the fragment .
Things which you have to do is Override onCreate() method in your fragment and this is how you will receive the bundle.
private String mParam1; // these are global variable to get the data
private String mParam2; // after you receive the data, it can be used under `onActivityCreated()`
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1,null);
mParam2 = getArguments().getString(ARG_PARAM2,null);
// I have updated the code thisis how you can check which string is empty.
// cause if you try to do some work on null object you will get error
if(mParam1 == null){
// it is empty
}else{
// it is not empty
}
if(mParam2 == null){
// it is empty
}else{
// it is not empty
}
}
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// here you can use mParam1 and mParam2
}
Update
with conversation I had with you, you can start your fragment though your activity like this to tell the fragment if the array is null or not
HomeFragment fragobj = new HomeFragment();
Bundle bundle = new Bundle();
if(jsonary.length()==0){
bundle.putBoolean("isAvailable", false);
}else{
bundle.putBoolean("isAvailable", true);
}
fragobj.setArguments(bundle);
and in the onCreate() method of your fragment this is how you can read the boolean value
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
boolean mParam1 = getArguments().getBoolean("isAvailable",false);
}
}
Updated
ok now i realize what you are doing (fragment is already started before setting the bundle), you can try this. (it is quite cheeky )
make a method inside your HomeFragment
public static void setData(boolean isAvailable){
// here you will get the actual data
}
Now in your Activity when you get the JSONArray depending on that you can that you can do this.
HomeFragment.setData(true/false)
From your question I think you are getting user_login_id as null from bundle
Bundle bundle = this.getArguments();
strtext = bundle.getString("user_login_id");
This is because you have not put any string with user_login_id in your bundle
if(jsonary.length()==0)
{
// Toast.makeText(MainActivity.this,"Null",Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("nofrndsavailable", nofrndthere);
bundle.putString("user_login_id", <userid>); //put user id here
HomeFragment fragobj = new HomeFragment();
fragobj.setArguments(bundle);
}
else if(jsonary.length()!=0)
{
// Toast.makeText(MainActivity.this,"Not Null",Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("frndsavailable", frndthere);
bundle.putString("user_login_id",<userid>);// put user id here
HomeFragment fragobj = new HomeFragment();
fragobj.setArguments(bundle);
}
I have created a fragment I would like to add multiple instances of:
public class myFragment extends Fragment {
int id;
public static myFragment newInstance(int id) {
myFragment fragment = new myFragment();
int id = id;
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView idText = (TextView)
rootView.findViewById(R.id.idText);
idText.setText(id);
return rootView;
}
In my Main class,
private int currentID;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
currentId = 0;
FragmentManager manager = getFragmentManager();
FragmentTransaction fragmentTransaction =
manager.beginTransaction();
oneFragment = new myFragment();
oneFragment.newInstance(currentId);
currentId = 2;
fragmentTransaction
.replace(R.id.container, oneFragment)
.commit();
twoFragment = new myFragment();
twoFragment.newInstance(currentId);
currentId = 3;
The problem is, oneFragment is coming up with id 2. I am not entirely sure why this is the case. The container is a LinearLayout with a vertical orientation and the Fragment is a linear as well. I would like to use the code I wrote for myFragment repeatedly for all of the ID's.
Your MyFragment class needs to transfer the id like this:
public class MyFragment extends Fragment {
private final static String ID_KEY = "id_key";
public int id;
public static MyFragment newInstance(int id) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(ID_KEY, id);
fragment.setArguments(args);
return fragment;
}
public MyFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
id = getArguments().getInt(ID_KEY);
}
}
}
I don't think that you need to create a fragment and then call newInstance() on it. I think you can just get away with calling newInstance(). Try this:
private int currentID;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
currentId = 0;
FragmentManager manager = getFragmentManager();
FragmentTransaction fragmentTransaction =
manager.beginTransaction();
MyFragment oneFragment = MyFragment.newInstance(currentId);
currentId = 2;
fragmentTransaction
.replace(R.id.container, oneFragment)
.commit();
MyFragment twoFragment = MyFragment.newInstance(currentId);
currentId = 3;
You should pass id for each Fragment using Constructor. So, every fragment instance hold their id in member variable
public class MyFragment extends Fragment {
private int id;
// constructor
public MyFragment() {
}
public MyFragment(int a) {
this.id = a;
}
// this method is not requreied if you use above Constuctor
public myFragment newInstance(int id) {
return new myFragment(id);
}
}
Main Class :
private int currentID = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
currentId = 0;
FragmentManager manager = getFragmentManager();
FragmentTransaction fragmentTransaction =
manager.beginTransaction();
oneFragment = new myFragment(currentID); // id = 0
currentId = 2;
fragmentTransaction
.replace(R.id.container, oneFragment)
.commit();
twoFragment = new myFragment(currentID); // id = 2
currentId = 3;
}
call fragment from fragment. I need to pass a string to the fragment. how to do it?
if (position==1){
FragmentTransaction ft;
VideoList lf = new VideoList();
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentsPanel, lf);
ft.addToBackStack(null);
ft.commit();
}
I want to pass String str = "absd"; and the second fragment to take String str1 = row of the first fragment
Use arguments!
public static VideoList videoListWithString(String string) {
VideoList videoList = new VideoList();
Bundle arguments = new Bundle();
arguments.putString("testString","test");
videoList.setArguments(arguments);
return videoList;
}
and in your fragments onCreate...
Bundle arguments = getArguments();
String testString = arguments.getString("testString");
You can use setArguments() and getArguments() methods in Fragments
like
if (position==1){
FragmentTransaction ft;
VideoList lf = new VideoList();
Bundle bundle = new Bundle();
bundle.putString("str", "absd");
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentsPanel, lf);
ft.addToBackStack(null);
ft.commit();
}
and in the fragment you get this string like this
public class VideoList extends ListFragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String str=bundle.getString("str");
}
}
Bundle params = new Bundle();
params.putString("str1", "absd");
getFragmentManager().beginTransaction()
.replace(R.id.fragment_place, YourFragment.instantiate(getActivity(), YourFragment.class.getName(), params), "YourFragmentTag").commit();
If your requirement is to pass value from one fragment to another then try using bundle.
For example:
TalkDetail fragment = new TalkDetail();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("largeimg", largeimg);
bundle.putString("excert", excert);
bundle.putString("description",description);
bundle.putString("cat", cat);
bundle.putString("header_title", "Talk");
//bundle.putInt("postid", postid);
fragment.setArguments(bundle);
((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
Here's your BaseContainerFragment class that will help to get better back track and other good stuff
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
In my application there are several fragments in an activity and I am maintaining a backStack for these fragment. Everything is okay, but there is a nested fragment among them. When I put it into backStack and again resume in by pressing back button, the fragment looks overllaping previous content (child fragment).
This is the normal view:
This is the screenshot of overlapping view (when I resume the fragment):
You can get the difference as the second one's text is more deep (that means child fragment is overlapping)
How can I avoid that? This the code for my nested fragment:
public class CompetitiveProgramming extends SherlockProgressFragment implements
OnChapterSelectListener, OnSubChapterSelectListener {
View mContentView;
static public List<Chapter> chapterList = new ArrayList<Chapter>();
private ProcessTask processTask = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContentView = inflater.inflate(
R.layout.competitive_programming_exercise, container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setContentShown(false);
setContentView(mContentView);
processTask = new ProcessTask();
processTask.execute();
}
protected class ProcessTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// background task
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
if (mContentView.findViewById(R.id.fragment_container) != null) {
getChildFragmentManager().beginTransaction()
.add(R.id.fragment_container, new ChaptersListFragment()).commit();
} else {
transaction.add(R.id.category_fragment, new ChaptersListFragment());
transaction.add(R.id.sub_category_fragment, new SubChaptersListFragment());
transaction.add(R.id.sub_sub_category_fragment,
new SubSubChaptersListFragment());
}
transaction.commit();
setContentShown(true);
}
}
static protected class Chapter {
String chapterTitle;
List<SubChapter> subchapterList;
public Chapter(String chapterTitle, List<SubChapter> subchapterList) {
this.chapterTitle = chapterTitle;
this.subchapterList = subchapterList;
}
}
#Override
public void onChapterSelected(int position) {
SubChaptersListFragment subChaptersListFrag = (SubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_category_fragment);
if (subChaptersListFrag != null) {
subChaptersListFrag.updateList(position);
} else {
SubChaptersListFragment subChapterFragment = new SubChaptersListFragment();
Bundle args = new Bundle();
args.putInt(SubChaptersListFragment.CHAPTER_POSITION, position);
subChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subChapterFragment);
// transaction.addToBackStack(null);
transaction.commit();
}
}
#Override
public void onSubChapterSelected(int prev, int position) {
SubSubChaptersListFragment subSubChaptersListFrag = (SubSubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_sub_category_fragment);
if (subSubChaptersListFrag != null) {
subSubChaptersListFrag.updateList(prev, position);
} else {
SubSubChaptersListFragment subSubChapterFragment = new SubSubChaptersListFragment();
Bundle args = new Bundle();
args.putIntArray(SubSubChaptersListFragment.POSITIONS, new int[]{prev, position});
subSubChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subSubChapterFragment);
// transaction.addToBackStack(null);
transaction.commit();
}
}
#Override
public void onStop() {
super.onStop();
if (processTask != null && processTask.getStatus() != AsyncTask.Status.FINISHED) {
processTask.cancel(true);
}
}
}
Kirill Kulakov is right. replace instead of add should be used. I edited the code:
public class CompetitiveProgramming extends SherlockProgressFragment implements
OnChapterSelectListener, OnSubChapterSelectListener {
View mContentView;
static public List<Chapter> chapterList = new ArrayList<Chapter>();
private ProcessTask processTask = null;
Fragment chapterFragment = null;
Fragment subChapterFragment = null;
Fragment subSubChapterFragment = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContentView = inflater.inflate(
R.layout.competitive_programming_exercise, container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setContentShown(false);
setContentView(mContentView);
processTask = new ProcessTask();
processTask.execute();
}
protected class ProcessTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// background task
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
chapterFragment = new ChaptersListFragment();
if (mContentView.findViewById(R.id.fragment_container) != null) {
transaction.replace(R.id.fragment_container, chapterFragment);
} else {
subChapterFragment = new SubChaptersListFragment();
subSubChapterFragment = new SubSubChaptersListFragment();
transaction.replace(R.id.category_fragment, chapterFragment);
transaction.replace(R.id.sub_category_fragment, subChapterFragment);
transaction.replace(R.id.sub_sub_category_fragment, subSubChapterFragment);
}
transaction.commit();
setContentShown(true);
}
}
static protected class Chapter {
String chapterTitle;
List<SubChapter> subchapterList;
public Chapter(String chapterTitle, List<SubChapter> subchapterList) {
this.chapterTitle = chapterTitle;
this.subchapterList = subchapterList;
}
}
#Override
public void onChapterSelected(int position) {
SubChaptersListFragment subChaptersListFrag = (SubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_category_fragment);
if (subChaptersListFrag != null) {
subChaptersListFrag.updateList(position);
} else {
SubChaptersListFragment subChapterFragment = new SubChaptersListFragment();
Bundle args = new Bundle();
args.putInt(SubChaptersListFragment.CHAPTER_POSITION, position);
subChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subChapterFragment);
// transaction.addToBackStack(null);
transaction.commit();
}
}
#Override
public void onSubChapterSelected(int prev, int position) {
SubSubChaptersListFragment subSubChaptersListFrag = (SubSubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_sub_category_fragment);
if (subSubChaptersListFrag != null) {
subSubChaptersListFrag.updateList(prev, position);
} else {
SubSubChaptersListFragment subSubChapterFragment = new SubSubChaptersListFragment();
Bundle args = new Bundle();
args.putIntArray(SubSubChaptersListFragment.POSITIONS, new int[]{prev, position});
subSubChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subSubChapterFragment);
// transaction.addToBackStack(null);
transaction.commit();
}
}
#Override
public void onStop() {
super.onStop();
if (processTask != null && processTask.getStatus() != AsyncTask.Status.FINISHED) {
processTask.cancel(true);
}
}
}
Hope this would help!
You probably are adding the fragment in your onResume(), which is not what you want as each time you are resumed new fragment is added. Just fix your code's logic
I'm presented with this unpleasant message on this code
public static HelpDetailsFragment newInstance(int index)
{
HelpDetailsFragment detailFragment = new HelpDetailsFragment();
Bundle bundleArgs = new Bundle();
bundleArgs.putInt("index", index);
detailFragment.setArguments(bundleArgs);
return detailFragment;
} // newInstance()
public int getCurrentIndex()
{
return getArguments().getInt("index", 0);
}
I'm using this code here:
HelpDetailsFragment detailFragment = (HelpDetailsFragment) getFragmentManager()
.findFragmentById(R.id.helpDetailsFrame);
if((detailFragment == null) || (detailFragment.getCurrentIndex() != index))
{
detailFragment = HelpDetailsFragment.newInstance(index);
Log.i(TAG, "HelpListFragment Create and replace details fragment for item:"+index);
FragmentTransaction fragTrans = getFragmentManager().beginTransaction();
fragTrans.replace(R.id.helpDetailsFrame, detailFragment);
fragTrans.commit();
}
What em I missing here??