I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:
Intent intent = new Intent(this, SettingsAc
When you click your button you can have it call:
super.onBackPressed();
Finish closes the whole application, this is is something i hate in Android development not finish that is fine but that they do not keep up wit ok syntax they have
startActivity(intent)
Why not
closeActivity(intent) ?
Just don't call finish()
on your MainActivity
then this eliminates the need to Override your onBackPressed()
in your SecondActivity
unless you are doing other things in that function. If you feel the "need" for this back button then you can simply call finish()
on the SecondActivity
and that will take you to your MainActivity
as long as you haven't called finish()
on it
Button edit = (Button) view.findViewById(R.id.yourButton);
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(this, YourMainActivity.class);
startActivity(intent);
finish();
}
});
if you use fragment u should use
getActivity().onBackPressed();
if you use single activity u can use
finish();
try this code instead of finish
:
onBackPressed();