android fallback page refresh page data

1. The onResume() method
starts a new page in the activity or fragment page (referred to as the original page) (referred to as the new page). When the new page returns, the original page needs to refresh the data. Observing their life cycle, you can see that the new page is started. The original page will call the onPause(), and onStop() methods in turn. When the new page is closed and returned, the original page will call onStart(), onResume(), so put the loaded data in the onResume() method.

    @Override
	public void onResume() {
    
    
	   super.onResume();
	   getData();//加载数据
	}

2.startActivityForResult method
Three cases
a. Use the default code of the new page to close, and the original page will be refreshed.
Set the return code.
Start the new page

startActivityForResult(new Intent(context,NewActivity.class).putExtras(bundle), requestCode);

//Start a new page with the bundle when the new page is
closed, it will automatically call Activity.RESULT_CANCELED, no need to write the result code code on the new page

Refresh data from the original page:

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
		// TODO Auto-generated method stub
		if (requestCode == this.requestCode && resultCode == Activity.RESULT_CANCELED) {
    
    
			//刷新数据
		}
	}

b. Rewrite the default code when the new page is closed, and refresh the original page.
Set the result code to RESULT_OK and
override the finish() method

		@Override
	public void finish() {
    
    
		setResult(RESULT_OK);
		super.finish();
	}

Refresh data from the original page:

@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
		// TODO Auto-generated method stub
		if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
    
    
		//加载数据
		}
	}

c. The new page has specific operations, such as submitting the data, the original page will refresh the data.
You can set the requestCode on the new page. This is also the most common method.

3. Other methods
Interface callback, broadcast, observer mode

Guess you like

Origin blog.csdn.net/ShiXinXin_Harbour/article/details/112302824