-1

I am new to Android development and finding it difficult to understand how interfaces passes values between Activities.

I have created a Interface like,

public interface ValuePasser {
    void valueObtained(String value);
}

And In First Activity, I am setting value as

public class MainActivity extends AppCompatActivity implements ValuePasser {

ValuePasser valuePasser;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    valuePasser = (ValuePasser) this;
    valuePasser.valueObtained("test Value");


    Intent i = new Intent(this, Main2Activity.class);
    startActivity(i);

}

  @Override
  public void valueObtained(String value) {
      Log.d("TAG", "TAg");
   }
}

And In Second Activity, I am trying to get the value like

public class Main2Activity extends AppCompatActivity implements ValuePasser {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
 }

  @Override
  public void valueObtained(String value) {
      Toast.makeText(Main2Activity.this, value, Toast.LENGTH_SHORT).show();
  }
}

Its not working, Can you please say how interfaces must be used and how it holds the datas so that it will be useful for noobs like me.

Savin Sharma
  • 769
  • 5
  • 22
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
  • `interface` is not a Concept of `Android` and FYI `Android` is not a language its a tech. You can use a bunch of languages to develop for `Android` most widely `JAVA` and `Kotlin` . You need learn How interface work in `Java` . – ADM Dec 06 '18 at 05:52
  • 1
    What you are doing is not really the use case of using `interface` in android . Read [How do I pass data between Activities in Android application ...](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application). – ADM Dec 06 '18 at 05:54
  • take this and you will understand about interfaces https://www.youtube.com/watch?v=Yaa3QroWe7Q –  Dec 06 '18 at 06:38

2 Answers2

0

This is how you can use your interface between two Activity to pass data.

Note - Interface are mostly used if you have the state of the previous class in which you want to pass data. Here your activity in which you are receiving the data should start first and you need the Context of that activity in pause state. If your activity is destroy and you don't have the Context of the activity still you want to pass the data the it is better to use putExtra as interface won't work in that case.

This is the Activity where you will receive the data through interface

public class RecieverActivity extends AppCompatActivity implements ValuePasser {

public static ValuePasser valuePasserSecondActivity; // this will be static so that you can use it in your main activity from where you have to send the data

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    valuePasserSecondActivity = this;
 }

  @Override
  public void valueObtained(String value) {
      Toast.makeText(RecieverActivity.this, value, Toast.LENGTH_SHORT).show();
  }
}

This is your Activity From where you will send the data through interface

    public class SendDataActivity extends AppCompatActivity{

    private ValuePasser valuePasserFirstActivity; // your interface

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        valuePasserFirstActivity = RecieverActivity.valuePasserSecondActivity; // intialize it with the Reciever's end activity interface

        if(valuePasserFirstActivity!=null)
          valuePasserFirstActivity.valueObtained("test Value");

    }
  }
Savin Sharma
  • 769
  • 5
  • 22
  • 1
    Seriously !!!! ? `Main2Activity` is not loaded yet and you can not use its `Context` if its not loaded . This is not the way you pass data between Activities . – ADM Dec 06 '18 at 06:20
  • @ADM i was explaining the flow of interface. I know the `Main2Activity` should load first in order to take the response of `interface`. I have edited my answer as well. – Savin Sharma Dec 06 '18 at 06:30
  • If you already know that then you should not use this example to explain . This code i going to Blast .. – ADM Dec 06 '18 at 06:36
0

If you want to pass a String between activities you should pass an extra parameter inside your intent, using a unique identifier for your string.

In your parent activity:

Intent i = new Intent(this, Main2Activity.class);
i.putExtra("your_string_identifier", "string value");
startActivity(i);

In your destination activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  String yourString = getIntent().getStringExtra("your_string_identifier");
}

Otherwise, if you want to pass structured data you should use Parcelable interface as explained in this simple tutorial. Alternatively, you can use Parceler library by johncarl81 that eliminates much of the boilerplate of Parcelable interface. Let me know if this helps

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64