导航回以前的活动而不激活onCreate

时间:2016-05-05 03:10:28

标签: android navigation

我有2项活动,WebMainActivityMainActivity。在应用开始时,我想加载WebMainActivity,这将加载Webview。在Web应用程序的某个位置,我将执行Web App Interface以激活本机活动MainActivity,并在完成时返回WebMainActivity

我的问题是,当我在finish()末尾加MainActivity时,加载WebMainActivity时,它再次启动onCreate。我希望显示WebMainActivity的最后状态,而不是onCreate,它会重新加载我的网页。有没有办法做到这一点?

我的活动Java类:

public class WebMainActivity extends Activity  {

WebView mWebView;
/**
 * Instantiate the interface and set the context
 */
WebAppInterface(Context c) {
    mContext = c;
}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.content_main);

    mWebView = (WebView) findViewById(R.id.webview);

    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    mWebView.getSettings().setJavaScriptEnabled(true);
    WebAppInterface wai = new WebAppInterface(this);
    mWebView.addJavascriptInterface(wai, "Android");


    AlertDialog.Builder builder = new AlertDialog.Builder(WebMainActivity.this);
    builder.setTitle("Please enter the IP address");


    final EditText input = new EditText(getApplicationContext());
    input.setTextColor(getResources().getColor(R.color.black));
    input.setPadding(20, 0, 20, 0);

    input.setInputType(InputType.TYPE_CLASS_TEXT);
    builder.setView(input);


    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String surl = (input.getText().toString());
            mWebView.loadUrl(surl);
        }

    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();

}

public class WebAppInterface {
    Context mContext;
  /**
     * Show a toast from the web page
     */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void receiveInput(String sInput) {
        Toast.makeText(mContext, sInput, Toast.LENGTH_SHORT).show(); 


        Intent intent = new Intent(mContext, MainActivity.class);
        startActivityForResult(intent,1);

    }
}

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnConnect = (Button) findViewById(R.id.btn_connect);
    btnConnect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
         builder.setTitle("Test");
         builder.setMessage("Hello World");
         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                }
        });
      builder.show();

       }
    });
        //Turn on webview again
        finish();
    }
}

清单文件:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden"
        android:configChanges="keyboardHidden|orientation|screenSize">
        <meta-data android:name="android.support.PARENT_ACTIVITY"
            android:value=".WebMainActivity">
        </meta-data>
    </activity>
    <activity
        android:name="WebMainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>
</application>

0 个答案:

没有答案