뒤로 버튼 및 이전 활동 새로 고침
두 가지 활동이있는 경우 :
- 파일 목록 및 마지막 수정 시간
- 파일 편집 활동
사용자는 목록에서 파일을 선택하고 파일 편집 활동으로 이동합니다. 편집이 완료되면 사용자는 뒤로 버튼을 눌러 파일 목록으로 돌아갑니다.
목록이 다시로드되지 않으므로 방금 편집 한 파일 수정 시간에 대해 잘못된 값이 표시됩니다.
뒤로 버튼을 누른 후 파일 목록을 새로 고치는 적절한 방법은 무엇입니까?
이 예에서는 사용중인 데이터베이스가없고 ArrayAdapter 만 있다고 가정합니다.
한 가지 옵션은 첫 번째 활동의 onResume을 사용하는 것입니다.
@Override
public void onResume()
{ // After a pause OR at startup
super.onResume();
//Refresh your stuff here
}
또는 결과에 대한 활동을 시작할 수 있습니다.
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
secondActivity에서 데이터를 다시 보내려는 경우 :
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
데이터를 반환하지 않으려면 :
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
이제 FirstActivity 클래스 쓰기에 대한 코드를 다음과 같은 onActivityResult()
방법을
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
//Update List
}
if (resultCode == RESULT_CANCELED) {
//Do nothing?
}
}
}//onActivityResult
나는 onRestart ()가 이것을 위해 더 잘 작동한다고 생각합니다.
@Override
public void onRestart() {
super.onRestart();
//When BACK BUTTON is pressed, the activity on the stack is restarted
//Do what you want on the refresh procedure here
}
onRestart () 내에서 Activity가 다시 시작될 때 ( '뒤로 버튼 누름'이벤트에서 다시 호출 됨) 수행 할 작업을 코딩 할 수 있습니다.
예를 들어 onCreate ()에서 수행하는 것과 동일한 작업을 수행하려면 onRestart ()에 코드를 붙여 넣습니다 (예 : 업데이트 된 값으로 UI 재구성).
요점 :
@Override
public void onRestart()
{
super.onRestart();
finish();
startActivity(getIntent());
}
onResume()
활동 번호 1 의 메서드를 재정의하는 것이 좋으며 배열 어댑터를 새로 고치는 코드를 포함하면 다음을 사용하여 수행됩니다.[yourListViewAdapater].notifyDataSetChanged();
목록을 새로 고치는 데 문제가있는 경우 다음을 읽으십시오. Android 목록보기 새로 고침
이전 활동을 새로 고치려면이 솔루션이 작동합니다.
새로 고치려는 이전 활동에서 :
@Override
public void onRestart()
{
super.onRestart();
// do some stuff here
}
If not handling a callback from the editing activity (with onActivityResult), then I'd rather put the logic you mentioned in onStart (or possibly in onRestart), since having it in onResume just seems like overkill, given that changes are only occurring after onStop.
At any rate, be familiar with the Activity lifecycle. Plus, take note of the onRestoreInstanceState and onSaveInstanceState methods, which do not appear in the pretty lifecycle diagram.
(Also, it's worth reviewing how the Notepad Tutorial handles what you're doing, though it does use a database.)
The think best way to to it is using
Intent i = new Intent(this.myActivity, SecondActivity.class);
startActivityForResult(i, 1);
private Cursor getAllFavorites() {
return mDb.query(DocsDsctnContract.DocsDsctnEntry.Description_Table_Name,
null,
null,
null,
null,
null,
DocsDsctnContract.DocsDsctnEntry.COLUMN_Timest);
}
@Override
public void onResume()
{ // After a pause OR at startup
super.onResume();
mAdapter.swapCursor(getAllFavorites());
mAdapter.notifyDataSetChanged();
}
public void swapCursor(Cursor newCursor){
if (mCursor!=null) mCursor.close();
mCursor = newCursor;
if (newCursor != null){
mAdapter.notifyDataSetChanged();
}
}
I just have favorites category so when i click to the item from favorites there appear such information and if i unlike it - this item should be deleted from Favorites : for that i refresh database and set it to adapter(for recyclerview)[I wish you will understand my problem & solution]
Try This
public void refreshActivity() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
or in Fragment
public void refreshActivity() {
Intent i = new Intent(getActivity(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
And Add this method to your onBackPressed() like
@Override
public void onBackPressed() {
refreshActivity();
super.onBackPressed();
}
}
Thats It...
@Override
public void onBackPressed() {
Intent intent = new Intent(this,DesiredActivity.class);
startActivity(intent);
super.onBackPressed();
}
참고URL : https://stackoverflow.com/questions/5545217/back-button-and-refreshing-previous-activity
'Development Tip' 카테고리의 다른 글
jQuery : 버튼을 제외한 첫 번째 보이는 입력 / 선택 / 텍스트 영역을 찾는 방법은 무엇입니까? (0) | 2020.10.04 |
---|---|
JSR-303 @Valid 주석이 자식 개체 목록에 대해 작동하지 않습니다. (0) | 2020.10.04 |
날짜 문자열을 다른 형식으로 변환하는 방법 (0) | 2020.10.04 |
응용 프로그램 구성 파일의 연결 문자열 'MyConnection'에 필수 providerName 특성이 없습니다. " (0) | 2020.10.04 |
UITableViewCell, 스 와이프시 삭제 버튼 표시 (0) | 2020.10.03 |