I am still new to Android programming, i did search for checking the difference between Bundle in Activity and in Fragment and what are the use cases of these.
Please give me the difference with exact use case.
Thanks for your help :)
It is a different bundle.
When you create activity you get system or custom bundle (with save data).
When you create fragment you can put same date from activity (ex same id) but this will be a new bundle.
If you want same data in frament like super.onSaveInstanceState(savedInstanceState); in activity, you need to return this bundle to activity, and for restoring you must parse this data in activity and put to fragment.
Related
i need an approach how to handle this topic:
i have create a Mainactivity (A) and a Tabbed Activity (B) that contains 3 Tabs (Fragment One, Fragment Two and Fragment Tree)
how to send a string from the Mainactivity to the Fragment One without starting the Tabbed Activity.
when i use bundle
let say: in MainActivity
Bundle bundle=new Bundle();
bundle.putString("key", editText.getText().toString());
BlankFragmentTab1 fragment = new BlankFragmentTab1();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment).commit();
and inside Fragment One i get the String with getarguments.getString("key")
i get a nullpointerexception.
the approach works only by the communication between the Fragments and its container Activity.
thank you very much for your help
LieForBananas is correct.
You can not interact with your FragmentOne without calling its host activity.
However you can use SharedPreferences instead of Bundle for retrieving data.
I have an Activity that have 3 fragment(FragmentA, FragmentB, FragmentC) like sliding tab. From FragmentB call another activity (lets call ActivityBB). After get item from Activity BB, How I can get value from ActivityBB and bring back to previous FragmentB ???
Well there are three ways that just comes in my mind. There may be more. But for now let me tell you those.
On ActivityBB put the values that you want to save in SharedPreferences. And then restart your activity. Well yes this might only work if you have values that can be arranged in key-value pairs. And is also not the proper way to do things. But will get your job done.
To restart an activity use this code. and then get your values from SharedPreferences.
Intent intent = getIntent();
finish();
startActivity(intent);
You can implement interfaces. This method is the best way to communicate between fragments. For more details check the google's documentation.
http://developer.android.com/training/basics/fragments/communicating.html
You can use Bundles. For that check this link.
How to pass a value from one Fragment to another in Android?
You can try some like this..
Pass your value into intent.
This code in your ActivityBB
Intent intent = new Intent(ActivityBB.this,ActivityBB.class);
intent.putExtra("yourDataKey",yourData)
startActivity(intent);
After that get your value into ActivityAA and load your Fragment with desired data
Intent intent = getIntent();
String yourValue = intent.getExtra("yourDataKey");
I solved this use intent and bundel with this flow :
MainActivity(FragmentA, FragmentB, FragmentC)
This activty(eq : from FragmentB) pass data using intent to ActivityBB
ActivityBB
at onClick ListItem in this activity, I pass data using bundle and call MainActivity (because I want to back to my previous fragment with item value from ActivityBB)
I make a condition from bundle at onCreate method at MainActivity to display currentItem(viewPager)
Actually its work, but I think this is not the proper way. I hope there is a solution with proper way from anyone to solve this.
Is that compulsory to set data using bundle and set argument?
What is wrong Here?
MyFragment frag = new MyFragment ()
frag.setData(mSchoolData);
//add to back stack stuff.
by using the setArgs() you will ensure that this Fragment can be recreated due to lifecycle event...while by passing arguments with your own setters it may not work properly under certain circustances. That is why it is absolutely recommended to either uset Args OR use Intent extras, these will always be automatically provided by the system if the fragment gets recreated.
Is there a way to send a bundle to a fragment without actually putting that fragment into a container? I ask because I want to take a few Strings from an EditTexts input field and then call them in another fragment at a later time.
I have code like this
Bundle bundle = new Bundle();
Fragment fragment = new Assessment_Fragment();
bundle.putString("company", companyName);
bundle.putString("project", projectName);
fragment.setArguments(bundle);
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.header_fragment_container, fragment).commit();
In this code I pass the bundle with the transaction but I also replace the existing fragment in the "header_fragment_container". I don't want to do that. I just want to pass the bundled data to the specified fragment. When that fragment is eventually called I will receive the bundle. Is this possible?
Is this possible?
Sure. Just delete the last Java statement (getFragmentManager()....).
Now, as to whether it is sensible for you to create a fragment, then never add it to the fragment manager (either with a UI or not), that's another matter. Java supports lots of classes; Fragment is just one of them. If you need a generic data container, choose something lighter weight, such as just the Bundle itself.
I currently have an app that relies heavily on Intents and the extras given to them before starting the activity. The extras are used when calling a webservice which in turn supplies the content that needs to be shown
I am trying to convert that model, to one where I have a static Fragment (lets call it Player) at the bottom of my screen, and another Fragment (lets call it Content) above it which will show the main content. By selecting options on the main screen other content will be shown by replacing the Content Fragment.
But, these new Fragments are currently the Intents that rely on the extras so heavily.
Is there a way to replace a Fragment by a new one, but still be able to add extras to it?
If so, let's say I have the following piece of code:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_holder, new MusicAlbumList(), "albumlist");
ft.commit();
How would I add the extras to the MusicAlbumList?
If that's not possible, how will I get the data that's currently being passed through extras into my new Fragment before it force closes due to missing essential data?
Or you can do this
MusicAlbumList fragment = new MusicAlbumList();
Bundle args = new Bundle();
args.putString("StringName","Value here");
fragment.setArguments(args);
then do your replace stuff. Then in the fragments onStart or onCreate call this.getArguments(); to pull the bundle, then get your extras out of there.
Change the constructor of MusicAlbumList from default constructor to one with arguments like new MusicAlbumList(int arg1, ...) and pass the values you want to set through the constructor