I have an main activity hosting three fragments. I want to pass data from another activity(which hosts a form) to one of the fragment in the main activity. Also the main activity uses viewpager to host the three fragments. How is that possible?
That depends on data you want to pass, but I guess that packing data into Bundle instance will do the job. When you create intent to launch another activity, you can set arguments on it. And later use that arguments when creating the fragments.
Take a look at this(from Intent class):
public Intent putExtra (String name, Bundle value)
public Bundle getExtras ()
To pass data between activites:
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("tag", dataToPass);
startActivity(intent);
If you want to pass a custom object like for instance an ArryList<CustomObject>, your custom object class have to implement Parcelable.
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.
How can I use the text in textview in one fragment in mainactivity from another fragment in another activity(activity_main2) .
Basically you will have your FirstFragment communicate with your MainActivity, your MainActivity with your OtherActivity, and your OtherActivity with your SecondFragment. For the specifics see my answer here!
You can pass extras between activities.
Then, in your MainActivity, add:
Intent intent = new Intent(getBaseContext(), Activity_main2.class);
intent.putExtra("EXTRA_KEY_TEXT", "myText");
startActivity(intent);
And add to your Activity_main2:
String textFromMainActivity = getIntent().getStringExtra("EXTRA_KEY_TEXT");
Hope it helps you!
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.
To implement the up navigation, I would like to go back to a specific activity on the history stack. If the activities on the stack are implemented by different classes, it works like this (assuming I have activities A, B and C on the stack and want to go back to activity A:
protected void onUpPressed() {
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
}
Android will pop activities off the stack until the activity specified by the intent is the top most (activity implemented by class A in this case).
However, my app has several activities on the stack implemented by the same class. That's because they display the same kind of data but for different objects. They were launched with an intent that specified both the class implementing the activity and the object to display (either in the extras bundle or in the data property).
Now I'm looking for code to again pop several activities off the history stack until the matching activity the top most. If I extend the above code and additionally set the extras bundle or the data property, it doesn't work. Android always matches the first activity implemented by the specified class and doesn't go back far enough. The extras bundle and the data property are ignored.
protected void onUpPressed() {
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setData(Uri.parse("myapp:" + rootId));
startActivity(intent);
finish();
}
So how can I achieve to go back to a specific activity? What intent fields does Android compare to determine if it has found the desired activity?
As you described, you have one Activity that show different content based on the starting Intent. Well, why not Fragments?
I don't know the details of your applications architecture, but is should be easy to refactor that Activity to a Fragment. There could be a FragmentActivity that wraps all the Fragments which responsible for showing the content. This way you would have much more freedome to handle the activity's stack of fragments.
Steps summarized:
Convert your existing Activity (that show the content) to a Fragment.
Make a FragmentActivity (that will manage the Fragments).
Make the FragmentActivity "singleInstance", so it will cache all the
"startActivity" requests, where you have the opportunity to add a
new Fragment representing the new content to show.
You could add fragments this way:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// ContentFragment is the Fragment that you made from your Activity.
// Here you can pass the intent that stores the object to show also,
// so the parsing of the intent would be the same.
ContentFragment fragment = ContentFragment.newInstance(intent);
getFragmentManager()
.beginTransaction()
.add(fragment, null)
.addToBackStack("id here that will be used at pop")
.commit();
}
And you can pop to a specific id this way:
getFragmentManager().popBackStack("id here", 0);
This solution has a side-effect. The fragments will stick together, so you cannot insert any other Activity between them. This is trivial, but worth mentioning since differs from your current implementation.
I also assumed that you are familiar with how "singleInstance" and Fragments work. Fell free to ask if something is not clear.
To implement FLAG_ACTIVITY_CLEAR_TOP, Android searches through your task's activity stack (from top to bottom) using the following test:
if (r.realActivity.equals(newR.realActivity)) {
Your problem is that realActivity is a ComponentName (so the above comparison locates the topmost activity in the stack that matches on package and class name): no further test is performed against the intent, so it is impossible to be any more specific about which of that component's activities you wish to target.
Therefore:
So how can I achieve to go back to a specific activity?
There is no native means of accomplishing this. Your best bet is probably to manually implement a form of bubbling as suggested by #VM4.
What intent fields does Android compare to determine if it has found the desired activity?
Only the component name.
I recommend storing your data model outside of the extras, using a singleton to access, and refreshing an activity using that data model on the onResume()
Lets say you are in /home/usr/vm4/development and in this activity you have somekind of Views (lets say TextViews) that let you go to all parent directories. (Clicking on for example "usr" will go to /home/usr. In windows it looks like this:
I don't think Android lets you back into a specific Activity based on extra data. However you can do a trick here. The View (that when clicked takes you somewhere) can have a String tag attached to it:
TextView link = new TextView();
link.setTag("/home/usr");
Now when you click this View in the onClick() method:
onClick(View v) {
String extra = v.getTag();
// start your activity with extras here.
}
Now you just need to make sure you add the right tags to your links when inflating the activity.
I have an activity that is launched from another activity through an intent. The intent carries an extra "id" information. Now, the launched activity has a custom view (actually, a extension of LinearLayout class). I want to access the "id" information in the custom view. Can the activity pass that value to its contained view? Or can the view get a handle to the activity?
YES,
First you have pass id with the intent like
Intent i=new Intent(getApplicationContext(), sample.class);
i.putExtra("id", id);
startActivity();
it pass the value to sample class
here
String i=getIntent().getExtras().getSerializable("id").toString();
& you can use this id in your custom view
Yes, your custom View class can get a reference to the Activity it is contained in. Every View has a getContext() method which returns the Context the View is running in (i.e. your Activity).
make a method in your view that takes the id as a parameter, and then call that method from your activity.