我想让应用程序看起来像那张照片..
当我点击子活动上的MainActivity按钮时, OnActivityResult()和OnNewIntent()都在MainActicity中调用。
我打算调用OnActivityResult()从MenuActivity返回, 并调用OnNewIntent()从sub。
返回我了解到母亲活动通过StartActivityForResult()调用子活动, 和child调用setResult()进行返回。 当我想要呼叫的目标活动存活时, 我知道onNewContent()是必需的,因为客观活动不会调用onCrete()。
但我认为我错了。 Plz让我知道OnActivityResult()和OnNewResult()的区别是什么? 而且我想知道为什么调用OnActivityResult,即使在setResult()中没有调用setResult。
提前谢谢。
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();
}
});
}
}
答案 0 :(得分:2)
来自文档,它说:
这是为将launchMode设置为" singleTop"在他们的包中,或者如果客户端在调用startActivity(Intent)时使用了FLAG_ACTIVITY_SINGLE_TOP标志。在任何一种情况下,当在活动堆栈的顶部而不是正在启动的活动的新实例重新启动活动时,将在现有实例上使用用于重新启动的Intent调用onNewIntent()它
因此,如果您仔细阅读,只要活动的相同实例获得新意图,onNewIntent()
将始终被触发。在您的情况下,您发送了RESULT_OK意图,因此最终onNewIntent()
会收到它。
无论何时从父活动拨打startActivityForResult()
,父活动都会等待结果,父活动将返回前台的时间,即使我们没有将任何结果发送回activity,onActivityResult将被调用并将发送RESULT_CANCELED响应代码。这是如何设计这个链。
答案 1 :(得分:0)
简而言之, onNewIntent()会针对您的活动的任何意图进行调用。
OnActivityResult()返回 startActivityForResult()
启动的意图结果