How to pass objects between two activities? - android

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);

Related

How to get Activity from which second Activity was started?

I have activity A, MyRecyclerAdapter, and activity B.
In activity A I build MyRecyclerAdapter and start new activity B from recycler item click.
Now I need to access activity A in onDestroy method from activity B.
How can I do it?
Update:
I tried:
ActivityA parent = (ActivityA) getParent();
parent.setRead(id);
But it gives me null. I think it's because A is not a direct child of B;
For this purpose you should use startActivityForResult(intent). Then you override onActivityResult() in activity A to handle the data you receive after activity B was destroyed. In onDestroy() you'd just have to set the result with setResult(resultCode, data).
Like this you don't need to know about activity A in activity B.
I think you should create an interface class declare a method and implement it in Activity A. Then call that method from Activity B.
Alternatively you can create a Runnable in Activity A which does what you want in the run() method. Then pass the instance of that Runnable with the intent data to Activity B. Then call run on that instance object from Activity B. That should execute the code in the run() method in Activity A.

Passing data from fragment to non hosting activity

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);

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

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?

How to pass from an interface to other in Android

I'm a beginner in Android.
I created two interfaces with DroiDraw, then I added them res/layout/a.xml and res/layout/b.xml
My goal is passing from interface a to b.
How can I do??
If you want to pass data from Activity A to ActivityB, then add it to the Intent that starts Activity B
Intent i = new Intent(this,ActivityB.class);
i.putExtra("key",value);
startActivity(i);
Also have a look at the Bundle class.
If passing means "passing data" - look into SharedPreferences or Sqlite - it depends upon your options
If Passing means "navigating" - associate an activity with interface b - so launch activity b when the user wants to navigate to it from interface a.

Categories

Resources