onActivityResult () 전에 onResume ()이 호출됩니까?
내 앱이 배치되는 방법은 다음과 같습니다.
- onResume () 사용자에게 로그인하라는 메시지가 표시됨
- 사용자가 로그인하면 앱을 계속 사용할 수 있습니다. 3. 사용자가 언제든지 로그 아웃하면 다시 로그인하라는 메시지가 표시됩니다.
이것을 어떻게 할 수 있습니까?
내 MainActivity는 다음과 같습니다.
@Override
protected void onResume(){
super.onResume();
isLoggedIn = prefs.getBoolean("isLoggedIn", false);
if(!isLoggedIn){
showLoginActivity();
}
}
내 LoginActivity는 다음과 같습니다.
@Override
protected void onPostExecute(JSONObject json) {
String authorized = "200";
String unauthorized = "401";
String notfound = "404";
String status = new String();
try {
// Get the messages array
JSONObject response = json.getJSONObject("response");
status = response.getString("status");
if(status.equals(authorized)){
Toast.makeText(getApplicationContext(), "You have been logged into the app!",Toast.LENGTH_SHORT).show();
prefs.edit().putBoolean("isLoggedIn",true);
setResult(RESULT_OK, getIntent());
finish();
}
else if (status.equals(unauthorized)){
Toast.makeText(getApplicationContext(), "The username and password you provided are incorrect!",Toast.LENGTH_SHORT).show();
prefs.edit().putBoolean("isLoggedIn",true);
}
else if(status.equals(notfound)){
Toast.makeText(getApplicationContext(), "Not found",Toast.LENGTH_SHORT).show();
prefs.edit().putBoolean("isLoggedIn",true);
}
} catch (JSONException e) {
System.out.println(e);
} catch (NullPointerException e) {
System.out.println(e);
}
}
}
사용자가 성공적으로 로그인 한 후 :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "BOOM SHAKA LAKA!",Toast.LENGTH_SHORT).show();
}
}
문제는 onResume ()이 onActivityResult () 전에 호출되므로 사용자가 성공적으로 로그인하면 onResume ()이 먼저 호출되기 때문에 내 주요 활동에 알림이 표시되지 않습니다.
로그인을 요청하는 가장 좋은 장소는 어디입니까?
The call to onActivityResult happens before onResume, actually (see the docs). Are you sure you're actually starting the activity you wanted with startActivityForResult
and that you're setting the result of the invoked activity to RESULT_OK
before returning a value to your activity? Try just putting a Log
statement in your onActivityResult
to log that value and make sure that gets hit. Also, where are you setting the value of the isLoggedIn
preference? It seems like you should be setting that to true
in your login activity before it returns anyways, but that's clearly not happening.
Edit
The docs say:
You will receive this call immediately before onResume() when your activity is re-starting.
With fragments it isn't even as simple as onActivityResult()
being called before the call to onResume()
. If the activity that you are returning to was disposed of in the interim, you will find that calls to (for example) getActivity()
from onActivityResult()
will return null. However, if the activity has not been disposed of, a call to getActivity()
will return the containing activity.
This inconsistency can be a source of hard-to-diagnose defects but you can check the behaviour of your application by enabling the developer option "Don't keep activities". I tend to keep this turned on - I'd rather see a NullPointerException
in development than in production.
You may want to consider abstracting away the login state from the activity. For example if a user can post comments, let the onPost action ping for login state and go from there, instead of from the activity state.
참고URL : https://stackoverflow.com/questions/4253118/is-onresume-called-before-onactivityresult
'Programing' 카테고리의 다른 글
레거시 코드에 단위 테스트 추가 (0) | 2020.10.17 |
---|---|
내 div 여백이 겹치는 이유는 무엇이며 어떻게 수정할 수 있습니까? (0) | 2020.10.17 |
지연 불가 대 초기 즉시 지연 가능 (0) | 2020.10.17 |
MATLAB에서 동일한 파일에 스크립트와 함수 정의가있을 수 있습니까? (0) | 2020.10.17 |
현재 클립 보드 콘텐츠를 받으시겠습니까? (0) | 2020.10.17 |