我想让应用程序看起来像那张照片..
当我单击子活动上的 MainActivity 按钮时,在 MainActicity 中都会调用 OnActivityResult() 和 OnNewIntent()。
我打算调用 OnActivityResult() 从 MenuActivity 回来,并调用 OnNewIntent() 从 subs 回来。
我了解到母活动通过 StartActivityForResult() 调用子活动,子活动调用 setResult() 进行返回。当我要调用的客观活动还活着时,我了解到需要 onNewContent(),因为客观活动不调用 onCrete()。
但我觉得我错了。请让我知道 OnActivityResult() 和 OnNewResult() 的区别是什么?我还想知道为什么在 setResult() 中未调用 setResult 时调用 OnActivityResult。
先感谢您。
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(子活动之一,代码与其他子活动完全相同)
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();
}
});
}
}