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:
- Android: Go back to previous activity,
- How to finish current activity in Android,
- Button to go back to MainActivity
- android - How to close an activity on button click?
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.