问题
How to refresh current Activity 1 time after load? My code refresh the activity, but in Loop.
This site have some examples, but most is after click button. Isn't my case..
My code is:
finish();startActivity(getIntent());
sry my english..
回答1:
just do this way.
startActivity(getIntent());
finish();
回答2:
public class MainActivity extends Activity { String url;
//initializing WebView
private WebView mwebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Div1().execute();
String tag = (String) getIntent().getSerializableExtra("tag");
Intent intent = getIntent();
url = "https://xxxxxxxx.com/profile/"+tag+"";
//WebView
mwebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = mwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webView performance
mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
//mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mwebView.getSettings().setAppCacheEnabled(false);
mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
mwebView.loadUrl("https://xxxxxxxx.com/profile/"+tag+"");
//force links open in webview only
mwebView.setWebViewClient(new MyWebviewClient());
}
private class MyWebviewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Uri.parse(url).getHost().equals("xxxxxxxx.com/profile/\"+tag+\"")){
return false;
}
else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}}
// Div1 AsyncTask
private class Div1 extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
return null;
}
@Override
protected void onPostExecute(String result) {
mwebView.loadUrl("javascript:(function(){"+
"l=document.getElementsByTagName('BUTTON')[1];"+
"e=document.createEvent('MouseEvent');"+
"e.initEvent('click',true, true);"+
"l.dispatchEvent(e);"+
"})()");
}
}
}
回答3:
Button ok = (Button) findViewById(R.id.btnOK2);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(getIntent());
finish();
}
});
回答4:
This is sample code I wrote:
public class MainActivity extends AppCompatActivity {
static boolean isInit = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isInit) {
isInit = false;
startActivity(new Intent(this, MainActivity.class));
finish();
Log.d("Restart", "asdasda");
}
}
}
One thing I need to say is that if you start this activity from another activity, in every call you need set the isInit variable true like this:
MainActivity.isInit = true;
startActivity(new Intent(this, MainActivity.class));
Using "getIntent()" instead of "new Intent(this, MainActivity.class)" cause closing the application after logging "Restart" text.
I don't know whether it is what you want (I think this can not be any solution of any problem). I still think better is reload only views.
来源:https://stackoverflow.com/questions/45579788/how-to-refresh-current-activity-once-after-load