Reputation: 27
I have created Login form and when login success. It will go to MainActivity.java
in MainActivity
have a button
to go to Account Settings with intent
sending username and password. Like this:
editu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, EditUser.class);
i.putExtra("id",mUser.getId());
i.putExtra("username",mUser.getUsername());
i.putExtra("pass",mUser.getPassword());
startActivity(i);
//finish();
}
});
And in EditUser.class
has the Bundle
to get information like:
Bundle args = getIntent().getExtras();
But when I finished managing on account I want to go back to home (MainActivity) by tab on home button
<- (picture in link below). It will go out of the Application.
And I realized that because of this condition in MainActivity
if (null == args) {
Toast.makeText(this, getString(R.string.welcome_error_message),
Toast.LENGTH_SHORT).show();
finish();
}
But I don't know, how to send an intent
back from EditUser.class
to MainActivity.class
I have tried the following code, but it didn't work. (Code on EditUser)
public void onBackPressed() {
Intent i = new Intent(this,MainActivity.class);
i.putExtra(User.Column.ID,mUser.getId());
i.putExtra(User.Column.USERNAME,mUser.getUsername());
i.putExtra(User.Column.PASSWORD,mUser.getPassword());
startActivity(i);
}
Upvotes: 0
Views: 708
Reputation: 3193
First you need to call the MainActivity
using startActivityForResult(intent, requestCode);
. Then you do like the following code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
// Do something
}
}
EDIT:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedido);
// You have to enable the home button on action bar doing the following
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Then you must also do:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
else {
return MenuActionBar.optionsItemSelected(this, null, item)
? true : super.onOptionsItemSelected(item);
}
}
Upvotes: 1
Reputation: 409
You have to use onActivityResult() for this. set data to intent and pass it with click of back button click.
Upvotes: 1