0

I am creating an app, which contains 3 activities, say, A, B and C. Activity A is the main activity, which launches when the app opens.

Activity A is called Activity B using a startActivityForResult(intent, 1); Activity B returns a result, which is successfully captured, and within onActivityResult of Activity A, I am launching Activity C using startActivityForResult(intent2, 2);

Here is my onActivityResult for Activity A

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    Log.d("MAIN","Inside on activity result requestCode is : " + requestCode);
    if(requestCode == 1){
        if(resultCode == RESULT_OK){
            Log.d("MAINACT", "Inside RESULT CODE OK");
            if(data.getStringExtra("puzzleID")!=null){
                Log.d("MAINACT", "Inside puzzle has been selected");
                String puzzleIdResult = data.getStringExtra("puzzleID");
                Intent puzzleIntent = new Intent(MainActivity.this, TrialActivity.class);
                puzzleIntent.putExtra("puzzleID", puzzleIdResult);
                startActivityForResult(puzzleIntent, 2);
            }
         }
    }else if(requestCode == 2){
         Log.d("MAINACT", "Inside request code is 2 result code is : " + resultCode);
         if(resultCode == 0){
             if(data!=null){
             Log.d("MAINACT", "Inside STATUS NOT NULL");
             String puzzleStatus = data.getStringExtra("status");
             Log.d("MAINACT", "Puzzle status retrieved is : " + puzzleStatus);
             if(puzzleStatus.equals("pause")){
                 Log.d("MAINACT", "Inside puzzle status is paused");
             }
    }else{
          Log.d("MAINACT", "Data is null!!");
    }
}

This is the return result Intent from Activity C

@Override
protected void onPause() {
    super.onPause();
    Log.d(TRIALTAG, "On Pause CALLED");
        Log.d(TRIALTAG, "Sending Intent");
        String puzzleID = Integer.toString(puzzle_ID);
    Intent returnPause = new Intent();
    returnPause.putExtra("status", "pause");
    returnPause.putExtra("puzzleid", puzzleID);
    if(getParent() == null){
        setResult(Activity.RESULT_OK, returnPause);
    }else{
        getParent().setResult(Activity.RESULT_OK, returnPause);
    }
    finish();
 }

On returning from Activity C, I am getting a nullpointerexception, and the statement.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity 
{com.mypackagename.sudoku/com.mypackagename.sudoku.MainActivity}: java.lang.NullPointerException

I put in the data!=null check later to prevent a crash. I get Data is null!! now everytime I pause in Activity C. I tried the getParent method after reading through other solutions on SO, but that is not helping. Why is my data getting set in Activity B but not in Activity C?This is my first full fledged app, so please excuse any ignorance.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
kvnam
  • 1,285
  • 2
  • 19
  • 34

2 Answers2

0
@Override
protected void onDestroy() {
    Log.d(TRIALTAG, "On Pause CALLED");
    Log.d(TRIALTAG, "Sending Intent");
    String puzzleID = Integer.toString(puzzle_ID);
    Intent returnPause = new Intent();
    returnPause.putExtra("status", "pause");
    returnPause.putExtra("puzzleid", puzzleID);
    if (getParent() == null) {
        setResult(Activity.RESULT_OK, returnPause);
    } else {
        getParent().setResult(Activity.RESULT_OK, returnPause);
    }
    super.onDestoy();
}

try using this. i used onDestroy in place of onPause and removed finish. and called the supoer.onDestroy at the end.

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Wont onDestroy destroy my activity? I only want to pause it, and resume the activity when the user clicks on the continue button in Activity A. Can I still do that after an onDestroy? – kvnam May 04 '13 at 08:40
  • 1
    onactivityresult will be called when your called activity is finisehd.. to communicate between two runninga activity you need other process not this – stinepike May 04 '13 at 09:01
  • Ok I understand what you are saying, but I need to remove Activity C from view when it is paused, and display Activity A till user selects continue. finish() lets me do that, but I am guessing its not the best way. Thanks for the response, let me read up about this a little. – kvnam May 04 '13 at 09:25
0

It seems like setResult() method is not getting called since result code set as per exception is 0 (RESULT_CANCELED) and not -1 (RESULT_OK)

Are you sure your activity C is not crashing at Integer.toString(puzzle_ID)??

Debugging onPause() method of Activity C will help you resolve the crash.

P.S: That's why you should avoid using magic numbers like if(resultCode == 0)

P.S: FYI, RESULT_CANCELED (0) : If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.

Update

Setting result in onPause() or onDestroy wont work. Set result in either onBackPressed() or in onFinish()

See this result not set in onPause() using setResult() when pressing the Back button

Community
  • 1
  • 1
Anirudh
  • 389
  • 1
  • 7
  • I tried debugging onPause() and it is giving the right value for String puzzleID, as well as the int value, so it's not crashing there. Clearly setResult is not working, coz it's still giving resultCode = 0. Thanks for the response..am still unsure of what is wrong though.. – kvnam May 04 '13 at 08:57
  • Setting result in onPause() or onDestroy wont work. Set result in either onBackPressed() or in onFinish() http://stackoverflow.com/questions/10023969/result-not-set-in-onpause-using-setresult-when-pressing-the-back-button – Anirudh May 04 '13 at 09:27
  • Thank you so much for pointing me in the right direction, this worked like a charm. Figured the entire thing now. How do I select this bit as the answer, or if you can edit you earlier response I will accept that as the answer. – kvnam May 04 '13 at 10:31