I want to make an application looks like that photo..
When I click the button to MainActivity on sub-activities, OnActivityResult() and OnNewIntent() are all called in MainActicity.
I intend to call OnActivityResult() for coming back from MenuActivity, and to call OnNewIntent() for coming back from subs.
I learned mother activity calls child activity through StartActivityForResult(), and child calls setResult() for return. And when the objective activity that I want to call is alive, I learned onNewContent() is needed because objective activity doesn't call onCrete().
But I think I'm wrong. Plz let me know what is the difference of OnActivityResult() and OnNewResult()? and also I want to know why OnActivityResult is called even though setResult is not called in setResult().
Thank you in advance.
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
public static final int REQUEST_CODE_MENU = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(editText.getText().toString().length() != 0 &&
editText2.getText().toString().length() != 0){
Intent intent = new Intent(getApplicationContext(), MenuActivity.class);
startActivityForResult(intent, REQUEST_CODE_MENU);
} else {
Toast.makeText(getApplicationContext(),"ID와 비밀번호를 입력해주세요.", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_MENU) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "MenuActivity에서 온 Intent입니다.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "ERROR : The value of resultCode is not RESULT_OK", Toast.LENGTH_LONG).show();
}
} else if {
Toast.makeText(getApplicationContext(), "ERROR : The value of requestCode is not REQUEST_CODE_MENU (100)", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Toast.makeText(getApplicationContext(), intent.getStringExtra("from") + "에서 온 Intent입니다.", Toast.LENGTH_LONG).show();
}
}
MenuActivity.java
public class MenuActivity extends AppCompatActivity {
public static final int REQUEST_CODE_CUSTOM = 200;
public static final int REQUEST_CODE_REVENUE = 300;
public static final int REQUEST_CODE_PRODUCT = 400;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button custom = (Button) findViewById(R.id.custom);
custom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_cus = new Intent(getApplicationContext(), CustomActivity.class);
startActivityForResult(intent_cus, REQUEST_CODE_CUSTOM);
}
});
Button revenue = (Button) findViewById(R.id.revenue);
revenue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_rev = new Intent(getApplicationContext(), RevenueActivity.class);
startActivityForResult(intent_rev, REQUEST_CODE_REVENUE);
}
});
Button product = (Button) findViewById(R.id.product);
product.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_pro = new Intent(getApplicationContext(), ProductActivity.class);
startActivityForResult(intent_pro, REQUEST_CODE_PRODUCT);
}
});
Button tologin = (Button) findViewById(R.id.tologin);
tologin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_login = new Intent(getApplicationContext(), MainActivity.class);
setResult(RESULT_OK, intent_login);
finish();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case REQUEST_CODE_CUSTOM :
Toast.makeText(getApplicationContext(), data.getStringExtra("from") + "에서 온 Intent입니다.", Toast.LENGTH_LONG).show(); break;
case REQUEST_CODE_PRODUCT :
Toast.makeText(getApplicationContext(), data.getStringExtra("from") + "에서 온 Intent입니다.", Toast.LENGTH_LONG).show(); break;
case REQUEST_CODE_REVENUE :
Toast.makeText(getApplicationContext(), data.getStringExtra("from") + "에서 온 Intent입니다.", Toast.LENGTH_LONG).show(); break;
default :
Toast.makeText(getApplicationContext(), "ERROR occured.", Toast.LENGTH_LONG).show();
}
}
}
CustomActivity.java (one of sub activities, it's code is perfectly same with other sub-activities)
public class CustomActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom);
Button tomenu = (Button) findViewById(R.id.tomenu);
Button tologin = (Button) findViewById(R.id.tologin);
tomenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent from_cus = new Intent(getApplicationContext(), MenuActivity.class);
from_cus.putExtra("from", "CustomActivity");
setResult(RESULT_OK, from_cus);
finish();
}
});
tologin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent from_cus = new Intent(getApplicationContext(), MainActivity.class);
from_cus.putExtra("from", "CustomActivity");
from_cus.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(from_cus);
finish();
}
});
}
}
From The documentation, It says:
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
So, if you read it carefully, onNewIntent()
will always get triggered whenever same instance of the activity gets a new intent. In your case you're sending a RESULT_OK intent, so eventually onNewIntent()
will receive it.
And whenever you make startActivityForResult()
call from parent activity, the parent activity will wait for the result, the time parent activity will come back in foreground, even if we don't send any result back to the activity, onActivityResult will be called and will send RESULT_CANCELED response code. That's how this chain has been designed.