我正在尝试使用]调用活动>
startActivityForResult()
来自片段。该活动基本上会执行某些操作,然后将结果返回,但是发生的事情却是很奇怪的是,在被调用的活动完成之后,该片段调用了onStart,onResume,onActivityResult,onActivityResult,onResume,而我不明白为什么。这造成了很多麻烦。
这里是代码:
Class extends ListFragment { ... @Override public void onResume() { Log.i(TAG_LOG, "onResume"); SharedPreferences pref = getActivity().getSharedPreferences(PreferenceKeys.PREF_NAME, MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); long timePassed; // goes here if the trial time has expired if ((pref.getBoolean(PreferenceKeys.TRIAL_DONE, false) && pref.getString(PreferenceKeys.USERNAME, "").equals("")) || (!pref.getBoolean(PreferenceKeys.TRIAL, false) && pref.getString(PreferenceKeys.USERNAME, "").equals(""))) { Log.i(TAG_LOG, "onResume-first if"); if (pref.getBoolean(PreferenceKeys.ON_RESUME_CALL, true)) { Log.i(TAG_LOG, "onResume-first if-onResumeCall"); editor.putBoolean(PreferenceKeys.ON_RESUME_CALL, false); Intent intent = new Intent(getActivity(), LoginRegisterActivity.class); if (!pref.getBoolean(PreferenceKeys.TRIAL_DONE, false)) { intent.putExtra("showTrialButton", true); } startActivityForResult(intent, REQUEST_LOGIN_REGISTER); } } else if (!pref.getBoolean(PreferenceKeys.TRIAL_DONE, false) && mResponseReceiver == null && (timePassed = pref.getLong(PreferenceKeys.TRIAL_STARTING_TIME, 0)) != 0) { Log.i(TAG_LOG, "sono dentroooooo"); //this happens when the application has been force stopped, //since the receiver would never be notified and the trial would never end if ((System.currentTimeMillis() - timePassed) >= (30 * 60 * 1000)) { if (onUserActionListener != null) { onUserActionListener.onTrialEnded(); } } else { if (onUserActionListener != null) { onUserActionListener.onTrialStarted(); } } } super.onResume(); } ... @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.i(TAG_LOG, "onActivityResult"); if (requestCode == REQUEST_LOGIN_REGISTER) { if(resultCode == Activity.RESULT_OK){ String username; String password; SharedPreferences preferences = getActivity().getSharedPreferences(PreferenceKeys.PREF_NAME, MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); if ((!data.hasExtra(PreferenceKeys.USERNAME))) { if(data.getBooleanExtra(PreferenceKeys.TRIAL, false) && !preferences.getBoolean(PreferenceKeys.TRIAL, false)) { editor.putBoolean(PreferenceKeys.ON_RESUME_CALL, true).apply(); // this is used to show the log in button while mode is trial onUserActionListener.onTrialStarted(); } } else { // TODO: manage the connection with the server username = data.getStringExtra(PreferenceKeys.USERNAME); password = data.getStringExtra(PreferenceKeys.PASSWORD); onUserActionListener.onUserConnected(username, password); } } if (resultCode == Activity.RESULT_CANCELED) { //Write your code if there's no result } } } ... } public class LoginRegisterActivity extends Activity { private Button trialButton; public static final int REQUEST_CODE_REGISTER = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_register); final Intent intent = new Intent(); if (getIntent().hasExtra("showTrialButton") && getIntent().getExtras().getBoolean("showTrialButton")) { findViewById(R.id.trial_button).setVisibility(View.VISIBLE); } trialButton = findViewById(R.id.trial_button); trialButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { intent.putExtra(PreferenceKeys.TRIAL, true); setResult(RESULT_OK, intent); finish(); } }); } }
这是日志显示的内容
onStartonResumeonResume-first如果onResume-first if-onResumeCallonActivityResultonResume
我就是不明白。有人知道如何解决吗?
我正在尝试使用片段中的startActivityForResult()调用活动。该活动基本上会执行某些操作,然后将结果返回,但是发生的事情却真的很奇怪,...
如果发生这种情况,请不要与os混为一谈,因此,为了停止做出麻烦,您可以为结果数据制作一个包装,如下所示: