Issue
The problem:
I have the main activity that I want to callback when I press back button from either smartphone and actionbar on the second activity. But it always crash, it just work when I put a finish(); in the main activity, but If I do that then the back button from smartphone doesn't work properly.
MainActivity:
public class Principal extends SherlockActivity {
public static int THEME = R.style.Theme_Sherlock;
private Button entrar;
private Button cadastrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
//setTheme(Principal.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
entrar = (Button)findViewById(R.id.entrar);
entrar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startActivity(new Intent(Principal.this,LoginActivity.class));
finish();
}
});
cadastrar = (Button)findViewById(R.id.cadastrar_home);
cadastrar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(Principal.this, RegisterActivity.class);
startActivity(intent);
//finish();
}
});
}
SecondActicity:
public class RegisterActivity extends SherlockActivity{
protected void onCreate(Bundle savedInstanceState) {
setTheme(Principal.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case android.R.id.home:
// Do whatever you want, e.g. finish()
Intent intent = new Intent(this, Principal.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return true;
}
}
Already tried many ways and none works, only with finish();
Solution
In your Second Activity just remove your intent and startActivity stuff. Only need:
case android.R.id.home:
finish();
break;
finish() will remove that activity from the back-stack so don't use it when starting a new activity that you want the user to get back to by pressing the back button.
Answered By - Mattias Isegran Bergander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.