0

So, here's the scenario. There is one EditText which has its own TextWatcher set, used for setting the word count.

I also have a Navigation Drawer, Sliding, and in that have an option which launches a new Activity for result.

The result I want is the Target number of words the user wants to achieve, and then get the result from the user input and calculate the perecentage of target recived and set it to the text of a TextView in the Main Activity.

Now, the problem is :

Navigation Drawer has it's own ItemClickListener, and it exists as an independent View in the Activity. (Hidden, mostly, that is.)

And the Main Activity is different view.

How can I implement a correct OnActivityResultMethod so that I can return to the Activity's oncreate Method, techinically speaking, to the TextWatcher so that the calculation can be made and percentage be set.

Because the OnActivityResult is called automatically, so I cannot do anything to override it.

It has the data the app needs, but it is not called progmatically, so it cannot return values.

2 Answers2

0

You can try this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)        
    Intent i = getIntent();
    i.putExtras(data.getExtras()); // pass result data to onCreate()
    startActivity(i);
    finish();
}

Restarting the Activity is the only way to get back to onCreate().

EDIT:

Now, in your onCreate(), make a check:

Intent i = getIntent();
Bundle args = i.getExtras();
if(args != null){

    ....

}

and handle the data in the EditText here.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • How do I get the Data which is passed via OnActivityResult to the MainActivity, which is again called here with a getIntent() arguement. –  Mar 06 '15 at 15:02
  • Isn't it flawed that the code after finish() gets executed on the same activity? –  Mar 06 '15 at 15:06
  • I get what you are trying to do and I am trying that too. My question is, shouldn't the activity get destroyed when finish(); is called and if that is so, how does the code written after it gets executed? –  Mar 06 '15 at 15:09
  • `finish()` ends the current instance of the `Activity`, and it is called after all the code is executed, not before :) – Yash Sampat Mar 06 '15 at 15:10
  • if we don't call `finish()`, when you go back the same `Activity` will be there ... – Yash Sampat Mar 06 '15 at 15:11
  • But you are calling it on line number 1. And I don't know how to get data that is passed to the oncreate? Should I use simple approach or is there anything new? –  Mar 06 '15 at 15:12
  • I did just that but now when I Press Okay on the other activity so that the result can be passed, the App closes. –  Mar 06 '15 at 15:18
  • use the same `requestCode` in `onActivityResult()` that you have used in `startActivityForResult()` – Yash Sampat Mar 06 '15 at 15:21
  • Using the same request code, but the activity ends and doesnt start again. –  Mar 06 '15 at 15:25
  • I shifted the finish() call at the top and then the code, inside the OnActivityResult ust like you did when you answered, before the edit. It works. –  Mar 06 '15 at 15:29
0

If you launch the Activity with an startActivityForResult() you can get the result very easily with an onActivityResult().

Check the code in this post and try it out.

How to manage `startActivityForResult` on Android?

Community
  • 1
  • 1
YandraK999
  • 13
  • 6