-4

I made an application "Quiz", which has 4 activities. Main activity sends a String with your name from EditText to activity with first question. I have a problem here, because I don't know how to send this string immediately to final activity from main activity, but without going to final activity. I want to go to Activity with first question from main activity, then to activity with second question, and in the end I want to go to final activity. Thanks for your help!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pysia93
  • 31
  • 2
  • 1
    You cant, just pass it along in your intents from activity to activity if your activities go in sequence – tyczj Mar 27 '17 at 12:48
  • 1
    Have a look at `SharedPreferences`or use some kind of Singleton like `Application` or a `BaseActivity` to store your data and read it afterwards. – redead Mar 27 '17 at 12:50
  • You can use static variable also to do this – DkThakur Mar 27 '17 at 12:53
  • 1
    Use one Activity only. And have a Fragment inside it which gets swapped from time to time. The variable will remain in the Activity and can be accessed from any Fragment as desired. – Phantômaxx Mar 27 '17 at 13:01

4 Answers4

0

You could use static fields to pass data.

Inside your FinalActivity class you could add the following variable:

private static String NAME = "";

And with the following getters and setters:

public static String getName(){
    return NAME;
}

public static void setName(String name){
   NAME = name;
}
Xema
  • 1,796
  • 1
  • 19
  • 28
0

You can use the getter setter here at Application class so you can get the string data from anywhere were you want to. This is not the only way but i think it is also the easy way.

  public class MyApplication extends Application {

  private String someVariable;

  public String getSomeVariable() {
    return someVariable;
  }

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

add this in your manifest

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

hen in your activities you can get and set the variable like so:

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

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

Some Url which may help you Android global variable

You can use shared preference also but as per our requirement i don't recommend that to you

Android Shared preferences example

Community
  • 1
  • 1
Shubhank Gupta
  • 833
  • 2
  • 15
  • 35
0

You can use Broadcast Receiver for your requirement. In your activity from which you want to send data, do this way:

Intent intent = new Intent();
intent.setAction("fourthactivity");
sendBroadcast(intent);

And In your fourth activity, make a broadcast receiver which receive your intent :

public class IncomingReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("fourthactivity")) {
            System.out.println("GOT THE INTENT");
        }
    }
}

Tell me if this doesn't work or click right if it works for you.

Anchal Singh
  • 329
  • 3
  • 13
0

You can use SharedPreference to store the answers as you go from one activity to other and later compare all the answers in the FinalActivity in that way less complex coding and you will achieve your desired result.

Aniket Jadhav
  • 166
  • 11