0

I have two activities. The first (MainActivity) has a FloatingActionButton, which starts the second (AddEntryActivity) via Intent. After filling the form, the data should be sent via JSON (nested asyncTask class). The success of this asyncTask is evaluated by a method of AddEntryActivity.
If it fails, it toasts a message. If it was successful, it toasts a message (everything works up to this point) and should close this activity and return to MainActivity.

I read a lot of similar questions like:

and tried with:

finish();
//super.finish();
//self.finish();
//AddEntryActivity.finish();

or

startActivity(new Intent(getApplicationContext(), MainActivity.class));

as well as combinations of them or this:

onBackPressed();

But none of them approach works for me. Here is the call of the second activity in the MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openAddRecordActivity();
        }
    });
}

public void openAddRecordActivity() {
    Intent intent = new Intent(this, AddEntryActivity.class);
    startActivity(intent);
}

The success toast appears (see below), but the return to the first activity never takes place. And here is the called method (by onPostExecute() of the AsyncTask) of AddEntryActivity:

    void processResult(String result) {
        if (result!=null) {

            String state = result.split("\\|")[0];
            String msg = result.split("\\|")[1];

            if (state == "200") {
                Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();


                finish();
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            } else {
                Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getBaseContext(), "No Result!", Toast.LENGTH_SHORT).show();
        }
  }

I'm doing nothing fancy, but cannot see my mistake. If you need additional information (like Manifest), let me know.

kit
  • 1,166
  • 5
  • 16
  • 23
xentity
  • 129
  • 1
  • 2
  • 10
  • Can you try passing `Intent.FLAG_ACTIVITY_NEW_TASK` while starting the `AddEntryActivity` and use `onBackPressed()` method once you get success response message? – Uma Sankar Nov 18 '18 at 15:18
  • 1
    If you started your second activity from the main, and you want to pass a result back, you should be using `startActivityForResult` first, then `setResult` and `finish` in the next – OneCricketeer Nov 18 '18 at 15:20
  • 2
    I think your code for `finish()` is never called. You should use `if ("200".equals(state))` and not the `==` operator, when it comes to strings. – d4vidi Nov 18 '18 at 15:21
  • 1
    A simple finish() should return you to the previous activity. You should not start the main activity again because that will put 2 main activities in your back stack. What happens after the success toast? Does AddEntryActivity just stay on the screen? – Greg Moens Nov 18 '18 at 15:21
  • Can you show AddEntryActivity fully? – Muhammadjon Nov 18 '18 at 15:22
  • @d4vidi It is called, since the toast with msg "OK" appears. I also verified this branch with a console output in Android Studio. – xentity Nov 18 '18 at 17:26
  • @GregMoens Yes, it simply stay as it is. – xentity Nov 18 '18 at 17:27

1 Answers1

0

You can use startActivityForResult(new Intent(this, SecondActivity.class), 1010) to go to SecondActivity then as soon as the AsyncTask is Successfull, just send the result to the MainActivity along with data like,

Intent data = new Intent();
data.putExtra("mydata", "myData");
setResult(Activity.RESULT_OK, data);
finish();

then in your MainActivity,

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 1010 && resultCode == Activity.RESULT_OK) {
                //get your data from Intent
            }
    }
Darshan
  • 4,020
  • 2
  • 18
  • 49
  • Oh, wow. It is working. Just for my curiosity: why is passing a result necessary? Normally, the first activity do not need any feedback. The toast message for the user would be enough. – xentity Nov 18 '18 at 17:40
  • @xentity thats completely optional, its just a way to tell the previous activity that the result is ok or not & according to that, you can cross check in the first Activity's onActivityResult(); method. Also if the answer helped you, please accept it! :) – Darshan Nov 18 '18 at 17:42
  • Sure, I am supposed to accept this as a solution, but I am wondering, why my simple attempts did not work? In many other questions the solution was a simple finish() and I guess there is a more simple way. – xentity Nov 18 '18 at 20:33
  • @xentity that's because you were calling finish(); before calling startActivity();, due to this the Activity was killed & startActivity (); was never called! – Darshan Nov 19 '18 at 10:46