Passing data from fragment to non hosting activity - android

I have tried finding a answer for my question but could not find anything, so I have Activity A,Fragment A,Activity B.
Activity A is hosting Fragment A, and i need to pass some data from Fragment A to Activity B how can i make that possible? i Know how to pass data to the hosting activity but i'm getting confused when trying to pass it to Activity B, can some one post a example with explanation how to make it possible?

You can simply use getActivity() or getContext() for getting activity context.
In your Fragment A, do like this.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("key", "value");
// here you pass data
getActivity().startActivity(intent);

Related

Getting a Result from an Activity

To describe the situation, say that I have 3 activities: A, B, and C
and there is a button in activity A which starts activity B, and there is a button in activity C when it is clicked, it should send a result from activity C to activity A
My Question is...Is there a way to pass a result from C to A? If there is a way, what it is?
Note: It would be good if the way you give uses the methods startActivityForResult(...) and onActivityResult(...)
Thank you in advance
If you just using simple types like String objects you can use Bundle and supplementary variables in B,C activities. And transfer it from C->B->A using onActivity result. Or you could use Shared preferences.
There's a flag of Intent called FLAG_ACTIVITY_FORWARD_RESULT. Call:
Intent intent = new Intent(this, ActivityB.class);
startActiivtyForResult(intent);
when starting Activity B (by calling startActivityForResult(intent)).
When opening C, call:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActiivtyForResult(intent);
finish();
which means: open C, close B. Then, when closing Acitivity C, call:
setResult(123);
finish();
the result from C will go back to A.
You can use Intent to transfer data if you are navigating from Activity C to Activity A. Otherwise, I would suggest using an Interface, and then pass the data as parameter in a callback method present in Activity A. You said you wanted answers like onActivityResult, for that I guess the Activity C should exit or something to invoke onActivityResult in the Activity A

Go back to another or old Activity from a fragment in an activity

I have looked for this but, can't seem to find the answer that I need.
So, my scenario is, I have activityA that starts activityB using Intent. Then activityB sets up some values in a bundle and starts a fragment using fragment manager. Now, I want to go back to activityA when a button is click. I also want to pass some variables from the Fragment to activityA. How do I do this?
Currently, I just start just the activityA again by using intent with some bundle of data passed in from the fragment. This is not good, because I am restarting activityA again. I just want to go back like using OnResume() or something similar, so that my old data is still there in activityA again.
Thanks
You can either call finish like you would do from an Activity but like so:
getActivity().finish();
Or you can start the Activity and recall the previous one from the stack:
Intent i = new Intent(getActivity(), activityA.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

How to Pass object from sub activity to main activity?

From ActivityA I'm starting ActivityB.
In ActivityB I'm creating a new Serializable object.
After the object has been created I want to close ActivityB and pass the new object to ActivityA.
How can I do it?
start Activity B with startActivityForResult().
In activity B, when the object is created create an Intent to pack the object in:
Intent result = new Intent();
result.putExtra("result", object);
setResult(RESULT_OK, result);
Then you will receive that intent in the onActivityResult() method of Activity A, where you can extract it like so:
data.getSerializableExtra("result");
Start the Activity B using startActivityForResult method.
When you finish creating object call setResult in Activity B. Set Your Data in Intent. You don't need to finish this.
Override function onActivityResult in Activity A. This will be called when you call setResult in Activity B. You can receive the data from Intent passed from Activity B.
But most of the time, you need separate Activities if only you have different screens with different tasks. Otherwise accomplish the task within the same Activity. *(A Good and Standard Practice).*
after the object is created, create an intent object, put that object to that intent and then start activity A. In the Activity A's onRestart() get that intent and from that intent get that object.
Why are you doing that? If you're using activity B only for creating new object, you can do it in a plain simple java class. What are you trying to accomplish?

In Android, while switching between the screens the GPS data is lost

I have three activities, Activity1, Activity2, and Activity3.. Activity1 is the main activity from which am switching to other two activities. While I switch to Activity2 from main activity it starts properly, when I switch to Activity3 from main activity and come back to the Activity2 all previous data will be lost and it starts from starting.
Is there any other way to switch to the activities other than using the startActivity() method.
You can store the data in static variable in another class and can call these whenever required.. i think it will work
Hmm, you seem to need to pass information between Activities.
You should use a Bundle to pass information from Activities 1 to 3 to 2,
Or use startActivityForResult(Intent, int) if you're going from Activities 1 to 3 to 1 to 2.
Try this, hope it helps.
Sender.java:
Intent i = new Intent(this, Receiver.class);
i.putExtra("object_name", object);
startActivity(i);
Receiver.java:
Bundle extras = getIntent().getExtras();
Type _variable = extras.getTypeExtra("object_name");
Do note that getTypeExtra(...) requires you to specify the type of information you are about to retrieve.
ex. getStringExtra(...), getFloatExtra(...)

How to pass objects between two activities?

I want to pass an object from Activity B to Activity A.
Scenario:
- Activity A calls Activity B
- User selects item in Activity B
- Activity B passes an object to Activity A
How do I accomplish this? And in which method do I read in the passed object in Activity A?
you can start your intent using startActivityForResult on Activity A ... And when finishing Activity B declare a bundle and put your serializable object to your bundle and add it to your intent. On Activity A's onActivityResult method you can get this intent back and retrieve your bundle...
Refer to this sample below.
http://micropilot.tistory.com/1577
Intents are used to send data between 2 activities....If its serializable data than send it using:
Eg:
Intent myIntent = new Intent(home.this,dvd.class);
myIntent.putExtra(name, value);

Categories

Resources