0

For Android studio, my question is how can I write(Java Code) an activity where avtivity 1 calls activity 2, and activity one sends a number to activity 2. After that the activity 2 prints this number out and increase it by one and sends it back to activity 1

thanks in advance and sorry if this already asked someone else

HIBO
  • 11
  • 2
  • This is a reasonably basic concept of Android (and Java in general) programming. Maybe try reading a book such as http://www.amazon.co.uk/Learn-Java-Android-Development/dp/1430264543 ? – MTCoster Dec 03 '15 at 19:16

3 Answers3

1

From these tutorials you will be able to make 2 Activities communicating with each other

Starting Another Activity http://developer.android.com/training/basics/firstapp/starting-activity.html

Communicating with Other Fragments http://developer.android.com/training/basics/fragments/communicating.html

Getting a Result from an Activity http://developer.android.com/training/basics/intents/result.html

Raza Ali Poonja
  • 1,086
  • 8
  • 16
0

What you want to do is described here in the official documentation : http://developer.android.com/training/basics/intents/result.html

Climbatize
  • 1,103
  • 19
  • 34
0

You can use the "delegate" method :

1st activity :

public class Activity1 implements AsyncResponse {

    ...

    public void printAndIncrement() {
        Activity2 activity2 = new Activity2();
        activity2.delegate = this;
        activity2.doWhatIWant(3);
        // destroy activity2
    }

    public void processFinish(int result) {
       // do something with the result
    }

}

2nd activity :

public class Activity2 {

    public AsyncResponse delegate = null;

    ...

    protected void doWhatIWant(int num1) {

        Systel.out.println(num);
        delegate.processFinish(num1+1);     

    }

}

(interface) :

public interface AsyncResponse {
    void processFinish(int result);
}
Fundhor
  • 3,369
  • 1
  • 25
  • 47