如何返回父活动

时间:2013-06-11 11:34:42

标签: android android-activity

我有2个活动,呼叫1个活动,例如

  Activity 1
      Activity a
  Activity 2
      Activity a

我想从Activity a返回到从中调用它的活动。

目前我收到空指针异常,说活动a没有定义父活动。

活动1和2如下

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//         TextView urlTv = (TextView) view.findViewById(R.id.url);     
//         String url = urlTv.getText().toString();
           String url = projectsList.get(position).get("url");             
           //Intent intent = new Intent(Intent.ACTION_VIEW);
           Intent intent = new Intent(getApplicationContext(), LaunchReadMoreActivity.class);
           intent.putExtra("url", url);
           //intent.setData(Uri.parse(url));
           startActivity(intent);

          }

        });

活动A: 公共类LaunchReadMoreActivity扩展了Activity {

WebView myWebView;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.read_screen);

    Intent intent = getIntent();

    String url = intent.getStringExtra("url");  

    WebView myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    //webSettings.setJavaScriptEnabled(true);
    webSettings.setBuiltInZoomControls(true);


    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);
    myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    myWebView.setScrollbarFadingEnabled(false);

    try {
        // load the url
        myWebView.loadUrl(url);
    } catch (Exception e) {
        e.printStackTrace();
    }

//  //myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
//  myWebView.setInitialScale(50);
//  //myWebView.loadUrl(url);
//  myWebView.loadUrl(url);
//      

    setupActionBar();

}

    setupActionBar();

2 个答案:

答案 0 :(得分:1)

启动Activity a时,不要在Activity A和Activity B中调用finish()方法

答案 1 :(得分:1)

通过以下代码

调用Activity_1和Activity_2中的Activity_a

Intent intent = new Intent(this,Activity_a.class); startActivity(意向); //确保不完成当前活动,即不要在活动1或2中使用finish()方法; 然后在Activity_a中覆盖方法,

public void onBackPressed() {
        super.onBackPressed();
        getIntent().setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Activity_a.this.finish();
    }

跳这段代码将帮助您解决问题。