How to get activity from Activity in TabHost?

In my application, Activity A will launch TabActivity, and TabActivity has some activity in its content.

tabHost.newTabSpec("tab1").setContent( new Intent(this, ActivityB.class ) ) ... 

And ActivityB will receive some data from the user (i.e. select a contact), and I want ActivityA to receive this data. But, as you can see, it seems that I cannot runActivityForResult and onActivityResult to get the result.

So how can I get data in ActivityA from ActivityB? Thank you

+4
source share
2 answers

Again there is no answer to my question. So I have to solve it myself. My solution is ugly, but worked. As you might guess, we can write a singleton class to store this data. ActivityB will write data, and ActivityA will read it. Very easy.

On the contrary, I searched on this site and found some simple solution. Like here

+1
source

Create a class for the object you want to save (for example: ObjectYouWantToStore).

Extend android.app.Application.

 public class MyApp extends android.app.Application { private ObjectYouWantToStore mObject; public ObjectYouWantToStore getObject() {; return mObject; } public void setObject(ObjectYouWantToStore obj) { mObject = obj; } } 

Write the path to the Application in the manifest file.

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".MyApp"> 

Saving data in your activity B

 MyApp app = (MyApp)getApplicationContext(); app.setObject(ObjectYouWantToStore); 

Access to the same data in your activity A

 MyApp app = (MyApp)getApplicationContext(); ObjectYouWantToStore obj = app.getObject(); 
0
source

Source: https://habr.com/ru/post/1333399/


All Articles