Reputation: 3543
I am new in this area and have such situation that i have two pages:
MainActivity.axml
SecondActivity.axml
I have a button in MainActivity called BtnOther, in this button i want to navigate to SecondActivity. In SecondActivity i have button btnBackToExisting1stPage which i want to go back to MainActivity. Generally it works but:
After either BtnOther or android back arrow is clicked application is going to minimize on emulator and not staying in front. Why is that and how to avoid that?
I would like when pressing either BtnOther or back arrow to go back from SecondActivity to MainActivity BUT to open exact MainActivity state as was before. For instance if i would have text edit field on MainActivity and put some text before going to SecondActivity and then when going back from SecondActivity i want to see exact same form state of MainActivity instead of creating new as seem to be in my current code.
My current code:
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var btnOther = FindViewById<Button>(Resource.Id.Other);
btnOther.Click += BtnOther_Click;
}
private void BtnOther_Click(object sender, EventArgs e)
{
Intent nextActivity = new Intent(this, typeof(SecondActivity));
StartActivity(nextActivity);
Finish();
}
}
public class SecondActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Second);
var btnBackToExisting1stPage = FindViewById<Button>(Resource.Id.BackToExisting1stPage);
btnBackToExisting1stPage.Click += OnBackPressed;
}
private void OnBackPressed(object sender, EventArgs e)
{
OnBackPressed();
}
public override void OnBackPressed()
{
Toast.MakeText(this, "Back button Pressed", ToastLength.Short).Show();
base.OnBackPressed();
Finish();
}
}
Upvotes: 0
Views: 68
Reputation: 41
While starting second activity from button click, you are calling "finish()" which destroys the MainActivity and creates SecondActivity. Just remove/comment the "finish()" call. You will have the retained instance as it is in MainActivity even while clicking back button.
Upvotes: 2