-2

I have this mini-game and two activities. I used an intent to order my app to go from activity 1 (Just a menu screen) to activity 2. However, I want to return the game score from activity 2 back to activity 1 when the gameIsOver, but my second activity reads:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new DrawView (this));

}

}

So my activity is really run in the DrawView class which extends the View class. How may I return something back to activity 1 from a View Class?

Acy
  • 589
  • 2
  • 9
  • 25
  • 1
    `startActivityForResult` – Pavneet_Singh Feb 25 '18 at 14:01
  • please read about `interface`. That is one way to call activity method (in your issue ) without passing instance – Shayan Pourvatan Feb 25 '18 at 14:01
  • @Pavneet_Singh `DrawView` extends from `View`, `startActivity` not working for view. – Shayan Pourvatan Feb 25 '18 at 14:02
  • This link might help you. https://stackoverflow.com/questions/14292398/how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android – Uma Sankar Feb 25 '18 at 14:02
  • @ShayanPourvatan OP said `However, I want to return the game score from activity 2 back to activity 1` though you are also right about callback mechanism between view and activity, nice work – Pavneet_Singh Feb 25 '18 at 14:10
  • Possible duplicate of [How to pass data from 2nd activity to 1st activity when pressed back? - android](https://stackoverflow.com/questions/14292398/how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android) – ADM Feb 25 '18 at 14:22

1 Answers1

0

two things needed here:

first - Your custom view need to report it's score to the hosting Activity2

in order to achieve that make an interface called e.g.

public interface OnResult {
    void onResult(int result);
}

make your Activity2 implement OnResult, and let your custom DrawView class have a field OnResult resultCallback; , and let the activity put itself as this field. e.g.

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DrawView view = new DrawView (this); // "this" here as a Context object
    view.setResultCallback(this); // "this" here as the OnResult listener object
    setContentView(view);
}

and than, from your custom view when there is a result call your resultCallback.onResult() (that is, call the activity)

read more about callback mechanism via interfaces here

second - in your activity2 pass the answer back to activity 1

first of all, when starting activity2 from activity1 don't just start it using startActivity(), but start it for a result using startActivityForResult()

and in your activity2 implementation of onResult() from the first step, return the result via an intent and finish the activity -

Intent intent = new Intent();
intent.putExtra("editTextValue", "value_here")
setResult(RESULT_OK, intent);        
finish(); 

read more about passing return values from one activity to another here (via @Uma Sankar)

Re'em
  • 1,869
  • 1
  • 22
  • 28
  • Thanks for the response, I think this works but I never learned callbacks yet. Thanks for hitting me up with the callback mechanism link – Acy Feb 25 '18 at 15:59