开发者

Using ActivityGroup to switch between Views is causing StackOverflowError

开发者 https://www.devze.com 2023-04-13 09:42 出处:网络
I have a program with 4 different tabs. One of these tabs is an ActivityGroup which has a ListView in it. When I click on one of the list items, it switches to WebActivity:

I have a program with 4 different tabs.

One of these tabs is an ActivityGroup which has a ListView in it. When I click on one of the list items, it switches to WebActivity:

        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
              Intent intent = new Intent(TabActivity2.this, WebActivity.class);
              Bundle b = new Bundle();
              b.putString("URL", URLs[(int)id]);
              b.putString("p开发者_运维百科revActivity", "TabActivity2");
              intent.putExtras(b);
              replaceContentView("web", intent);
          }
        });
    }

    public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
    }

So now we're in the WebActivity class. Here's the code:

public class WebActivity extends ActivityGroup {
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);

        Bundle b = getIntent().getExtras();
        String URL = b.getString("URL");

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginsEnabled(true);
            mWebView.requestFocus();
        mWebView.loadUrl(URL);

        mWebView.setWebViewClient(new FirstTabWebViewClient());        
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        Bundle b = getIntent().getExtras();
        String retActivity = b.getString("prevActivity");
        if (retActivity == "TabActivity2") {
            Intent intent = new Intent(WebActivity.this, TabActivity2.class);
            replaceContentView("list_webpages", intent);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
    }

I had to implement my own onKeyDown because otherwise hitting the Back button resulted in closing the application rather than returning to the ListView.

So the StackOverflowError seems to be a structural problem in my program and I'm not sure how to go about resolving it. Any help would be appreciated.


I ended up using a FrameLayout in the same activity with both WebView and ListView and used visilibity to switch between them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号