0

I'm working on my first Android app, specifically in this case something to help me cook better. I have 2 activities that I'm currently struggling with, my MainActivity.java and whatCanIMake.java. When I start whatCanIMake from MainActivity using startActivity(intent), I'm able to pass objects using parcelables and generally use whatCanIMake. My issue is that I want to be able to pass back a list of everything that I've changed in my whatCanIMake activity. For example: I pass in a list of all the ingredients I currently have, and then whatCanIMake removes the ingredients that a recipe uses. I'd like to pass back the updated list of remaining ingredients.

The problem I'm having is with startActivityForResult(). Previously I was successfully able to use startActivity(), pass it the intent with the extra parcelables, and Bob's your uncle. When I changed over to startActivityForResult(), the new activity fails to launch, as the app crashes before hitting onCreate(). Obviously this points to my implementation of startActivityForResult(), so I've read basically every stack overflow question related to startActivityForResult() and now I'm here!

My code for starting whatCanIMake is below, as well as the onActivityResult() and corresponding code inside whatCanIMake.

It's just so odd to me that I can simply change out startActivityForResult() for startActivity() and the app works again! Thanks for any help in advance.

MainActivity.java:

    public void searchRecipe(View view) {
        crossViewVariables.setMakeSort('a');
        Intent recipe = new Intent(this, whatCanIMake.class);
        recipe.putExtra("Pantry",stock);
        recipe.putExtra("Cookbook",activeCookbook);
        recipe.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
        startActivityForResult(recipe,R.integer.search_by_recipe);
        //startActivity(recipe);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {


            case (R.integer.search_by_recipe)://comes from Recipe Search?
                if (resultCode == Activity.RESULT_OK) {
                    Bundle b = data.getExtras();
                    stock = b.getParcelable("Pantry");
                }
                if (resultCode == Activity.RESULT_CANCELED) {

                }


            case (R.integer.search_by_ingredient)://comes from Recipe Search?
                if (resultCode == Activity.RESULT_OK) {
                    Bundle b = data.getExtras();
                    stock = b.getParcelable("Pantry");
                }
                if (resultCode == Activity.RESULT_CANCELED) {

                }


            case (R.integer.make_recipe)://comes from seeRecipe?
                if (resultCode == Activity.RESULT_OK) {
                    Bundle b = data.getExtras();
                    stock = b.getParcelable("Pantry");
                }
                if (resultCode == Activity.RESULT_CANCELED) {
                    Toast.makeText(getApplicationContext(), "Cancelled", Toast.LENGTH_LONG).show();
                }

            case (R.integer.edit_pantry):
                if (resultCode == Activity.RESULT_OK) {
                    Bundle b = data.getExtras();
                    stock = b.getParcelable("Pantry");
                }
        }
    }

whatCanIMake.java:

    Intent result = new Intent();
    result.putExtra("Pantry", stock);
    setResult(Activity.RESULT_OK,result);
    finish();

LogCat:

FATAL EXCEPTION: main
                                                                           Process: com.example.schre.mememachine, PID: 26721
                                                                           java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                               at android.view.View.performClick(View.java:5697)
                                                                               at android.widget.TextView.performClick(TextView.java:10826)
                                                                               at android.view.View$PerformClick.run(View.java:22526)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                               at android.view.View.performClick(View.java:5697) 
                                                                               at android.widget.TextView.performClick(TextView.java:10826) 
                                                                               at android.view.View$PerformClick.run(View.java:22526) 
                                                                               at android.os.Handler.handleCallback(Handler.java:739) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:158) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                            Caused by: android.util.AndroidRuntimeException: FORWARD_RESULT_FLAG used while also requesting a result
                                                                               at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1884)
                                                                               at android.app.Instrumentation.execStartActivity(Instrumentation.java:1545)
                                                                               at android.app.Activity.startActivityForResult(Activity.java:4283)
                                                                               at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                               at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                               at android.app.Activity.startActivityForResult(Activity.java:4230)
                                                                               at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                               at com.example.schre.mememachine.MainActivity.searchRecipe(MainActivity.java:91)
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                               at android.view.View.performClick(View.java:5697) 
                                                                               at android.widget.TextView.performClick(TextView.java:10826) 
                                                                               at android.view.View$PerformClick.run(View.java:22526) 
                                                                               at android.os.Handler.handleCallback(Handler.java:739) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:158) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Aug 14 '17 at 21:12
  • I changed the filtering settings, I found the logs. I'll edit my question to include them! Thanks! – AssaultPotato Aug 14 '17 at 21:22

2 Answers2

0

Instead of:

startActivityForResult(recipe,R.integer.search_by_recipe);

Try this:

startActivityForResult(recipe, getResources().getInteger(R.integer.search_by_recipe));
MWU
  • 9
  • 2
0

Remove this line:

recipe.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);

You do not need it for startActivityForResult(), and it's what is causing your crash.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I just arrived at the same conclusion, I removed it and it worked! I just need to make sure I update the rest of my activity calls in order to fix them all. Thanks a ton! – AssaultPotato Aug 14 '17 at 21:35
  • I'm having the exact same problem, though I don't have such a flag either. What would be the cause? – Can Poyrazoğlu Aug 06 '18 at 15:13
  • @CanPoyrazoğlu: If you are getting `FORWARD_RESULT_FLAG used while also requesting a result` -- as shown in the stack trace in the question -- then something added `FLAG_ACTIVITY_FORWARD_RESULT` to your `Intent` that you used for `startActivityForResult()`. That is the root cause of the crash in the question. – CommonsWare Aug 06 '18 at 21:58
  • After asking a question, the culprit turned out to be something really obscure: I was using random many-digit numbers for activity request codes. It seems that Android doesn't like large numbers for request codes. I've changed the code to a 4 digit integer and it worked instantly. Really weird. – Can Poyrazoğlu Aug 07 '18 at 09:05
  • @CanPoyrazoğlu: There is a limit on the request code value when you extend from `FragmentActivity` (e.g., inheriting from `AppCompatActivity`): https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#startactivityforresult_4 – CommonsWare Aug 07 '18 at 10:45