0

I have 2 Activities.
When I click the Button in board Activity, main will open and execute, then send value (mark) to the board Activity

It works.

But I want also to send 3 arrays from the board Activity to the main Activity, as variables, according which Button is clicked (I have 3 Buttons in the board Activity, each array for Button).

This is the function I used in board Activity:

public void getMessage(View V) {
        // Create The  Intent and Start The Activity to get The message
        Intent intentGetMessage = new Intent(this, MainActivity.class);
        startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
    }


    // Call Back method  to get the Message form other Activity    override the method
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);


        // check if the request code is same as what is passed  here it is 2
        if (requestCode == 2) {
            // fetch the message String
            String message = data.getStringExtra("MESSAGE");
            // Set the message string in textView
            mm.setText(message);

and this is the submit function in the main Activity :

    public void submitMessage(View V) {
        String message = String.valueOf(mark);
        Intent intentMessage = new Intent();
        // put the message to return as result in Intent
        intentMessage.putExtra("MESSAGE", message);
        // Set The Result in Intent
        setResult(2, intentMessage);
        // finish The activity
        finish();

    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • so mm.setText(message); is doing nothing since message is null at that point? – Nanoc Nov 19 '15 at 13:00
  • always there is a value of "mark" from the second activity .. – Maimoonah Abu Adas Nov 19 '15 at 13:03
  • Your question's not clear. Is this the code you have at the moment, that's working, and you want to know how you can change it to send three arrays back instead of a single string? You've not said what you've tried so far to achieve this, what you've read, what the results are. Essentially there's no evidence of research. Personally for this I'd use a singleton class, and if you google that you'll find plenty of info. – Craig Graham Nov 19 '15 at 13:04
  • This code works ,but I want to send data to main activity how? – Maimoonah Abu Adas Nov 19 '15 at 13:04
  • Just add some extras to intentGetMessage as you do with intentMessage – Nanoc Nov 19 '15 at 13:08
  • No, I want the first class to send arrays ,now it doesn't send anything !Just it revives . . I tried the following : to write the submitMessage method in bored activity ,and the getMessage and onActivityResult in bored activity ,so each activity send and recive !but it doesn't work ,please help – Maimoonah Abu Adas Nov 19 '15 at 13:09

1 Answers1

0

The simplest way is to use a singleton, that is a class of which only one instance exists. And the simplest way to do that is to define a class containing three static arrays, with public (static) accessors. You can then reference this class from either activity, place arrays in from one and read them back from the other. For example,

public class SampleSingleton {
private static String[] _array1;
private static Integer[] _array2;
private static boolean[] _array3;


public static String[] get_array1() {
    return _array1;
}

public static void set_array1(String[] _array1) {
    SampleSingleton._array1 = _array1;
}

public static Integer[] get_array2() {
    return _array2;
}

public static void set_array2(Integer[] _array2) {
    SampleSingleton._array2 = _array2;
}

public static boolean[] get_array3() {
    return _array3;
}

public static void set_array3(boolean[] _array3) {
    SampleSingleton._array3 = _array3;
}

}

There are other more elegant, and complicated, methods and the answers to this question give a few pointers, but as a quick and simple approach this'll do.

Community
  • 1
  • 1
Craig Graham
  • 1,161
  • 11
  • 35
  • Thanks for answering me .....I've created this class . then how could I reference for it in my activity and send array values ?Using Intent also? – Maimoonah Abu Adas Nov 19 '15 at 13:43
  • `SampleSingleton.set_array1()`. Because it's all static, it simply exists- one copy only, without you needing to instantiate it or declare a variable of this type. – Craig Graham Nov 19 '15 at 13:53
  • What do you mean by whatever ? – Maimoonah Abu Adas Nov 19 '15 at 16:45
  • If you don't know how to set an array though a property accessor, you need to go do some tutorials. You need to know how to do the basic language before getting to grips with Android – Craig Graham Nov 19 '15 at 22:52
  • Yes, I did . .And I always read about anything I don't know . .Thanks .. I solved the problem by using : Bundle bundle =new Bundle(); bundle.putStringArray("qustion",q );//array of questions And it works finally . . – Maimoonah Abu Adas Nov 19 '15 at 23:10