How to Pass object from sub activity to main activity? - android

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?

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

TabHost : Start the activity without destory other activities

I am new to android, I am using the TabActivity. From the TabActivity I am starting the activity from intent. Order of Activity A - B - C - D then from the activity D, How can I create the same new Activity A (destroy the previous A). If i use the FLAG_ACTIVITY_REORDER_TO_FRONT its does not create the activity, instead open the last Activity A, If I use the Clear_top then it destroy the B and C Activity.
Please help to achieve this.
When you are calling Activity B from within activity A, call finish() after creating the new activity B using Intent. This will end the Activity A there. Then again from witin Activity D you can create a new Activity A using intent. Hope this helps.
You might want to consider destroying the previous activity before calling the next activity
so when you are going to call the activity B from A you might want to destroy the activity A using the keyword finish()
and likewise when you move from B to C and C to D and in the D activity destroy the activity of C and call the new activity A that way the A activity will get restarted.
finish(); //finish the current class
Intent intent = new Intent();
intent.setClass(getApplicationcontext(), nextclass.class); //specify the next class
startActivity(intent); //start the next class.
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
android:launchMode might be the answer you are looking for. From documentation:
Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent.
Means the existing the activity (if exist) will remain in the current stack untouched and new instance will be created at the top of the current stack. So when user back buttons, the user will see your activity A at the bottom of the stack as well.

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