How to Refresh Current Activity once after load?

℡╲_俬逩灬. 提交于 2021-02-07 11:14:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!