Activity B is closed because it launched from Activity A, this is normal behavior for android because Activity A needs to be restarted (singleTask) and all related instance will be kill/finish.
So, what you can do is implement shared-preference.
your activity A should be like this :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//// read share preference
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger("MYSHARE_PRFERENCE");
int isOpened = sharedPref.getInt("IS_ACTIVITY_B_ALREADY_OPENED", defaultValue);
if (isOpened == 1)
{
//resume activity B
startActivity(new Intent(MainActivity.this, ActivityB.class));
}
////
findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, ActivityB.class));
//// write on share preference
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("IS_ACTIVITY_B_ALREADY_OPENED", 1);
editor.commit();
////
}
});
}
}
P.S : I didn't compile it yet, the code might be error on compile. But share preference can be solution for your problem.