1

Bundles can be easily passed to new activities with intents:

Intent intent = new Intent(this, DisplayMessageActivity.class);
Bundle b = new Bundle();
// add extras...
intent.putExtras(b);
startActivity(intent);

Is it possible use Bundles (or similar) to send data to the main activity?

The Application class is created before any activity and I would like to use it to send data to the main activity. I do not want to have the data accessible globally or use static methods on the main activity to pass data to it.

Community
  • 1
  • 1
JustcallmeDrago
  • 1,885
  • 1
  • 13
  • 23
  • Not really sure what you are after. You need to understand that the `Application` class is only created once per process. This means, when you launch your app for the first time, `Application` class will be created and then your `MainActivity`. Now user presses BACK. Your `MainActivity` will finish. Now user starts your application again. If the process hasn't been reclaimed (killed), then Android will just create and launch a new instance of `MainActivity`. **It will not call `onCreate()` on the `Application` class again.** – David Wasser Jan 17 '14 at 17:44

2 Answers2

1

Since you already have a handle on the application, I would just use it to create an application variable -

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

You have to declare the class in your manifest like so:

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

And then in the receiver Activity:

// set
((MyApplication) this.getApplication()).setSomeVariable("foo");

// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();

As explained by Jeff Gilfelt here: Android global variable

Community
  • 1
  • 1
cgcarter1
  • 327
  • 1
  • 8
  • 1
    This is not what I am looking for. Accessing the data through the Application via getApplication() is possible in any activity, which as you said is basically a global variable. I do not want a global variable. I want the data available only to the main activity. – JustcallmeDrago Jan 16 '14 at 16:59
  • My bad, you did have that in your original post. – cgcarter1 Jan 16 '14 at 17:08
1

Have you considered temporarily using SharedPreferences and then deleting the SharedPreferences data in the onCreate of the new activity.

some example code:

Activity1:

SharedPreferences prefs = getSharedPreferences("mydata", 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("secretstring", "asdasdasdqwerty");
        editor.commit();

Activity2:

SharedPreferences prefs = getSharedPreferences("mydata", 0);
String savedString = prefs.getString("secretstring", "");

SharedPreferences prefs = getSharedPreferences("mydata", 0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("secretstring", "nope");
            editor.commit();

A second possibility is to use getters and setters for public variables but only return the correct variable to your main class by checking what class is asking for it.

Hope this helped :).

jimbob
  • 3,288
  • 11
  • 45
  • 70