Solved

switching views by button press?

Posted on 2013-01-22
2
337 Views
Last Modified: 2013-03-20
I have two buttons. Each opens a different URL.
The problem is returning from the webpages.

For the second button, it takes two presses of "return"
to navigate to the main page. How can I fix this?

package com.my.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button;
	private Button button1;

	public void onCreate(Bundle savedInstanceState) {
		final Context context = this;
		final Context context1 = this;

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.buttonUrl);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(context, WebViewActivity.class);
				intent.putExtra("google.com", "http://www.google.com");
             	startActivity(intent);
			}
		});
		
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent1 = new Intent(context1, WebViewActivity.class);				
				intent1.putExtra("yahoo.com", "http://www.yahoo.com");
				startActivity(intent1);
			}
		});
	}

}

package com.my.android;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

	private WebView webView;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);

		webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);
				
		Bundle extras = getIntent().getExtras();
		if (extras != null){
		String googleUrl = extras.getString("google.com");
		String yahooUrl = extras.getString("yahoo.com");
		if (googleUrl != null)
		    webView.loadUrl("http://www.google.com");
		else if (yahooUrl != null)
		    webView.loadUrl("http://www.yahoo.com");
		}
	}
}

Open in new window

0
Comment
Question by:pillmill
2 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 38809025
You might like to check the click count with getClickCount().

(MouseEvent method of course).
0
 
LVL 12

Accepted Solution

by:
PCableGuy earned 500 total points
ID: 38812646
Try changing your If Else logic in the WebViewActivity class and the Intent logic in the MainActivity class because it's possible that it's loading both Web pages. Use one String variable in the WebViewActivity, one Intent variable in the MainActivity

        private Button button;
      private Button button1;
      // put Intent as a Class variable
       private Intent intent;

        super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
        // Initialize Intent here instead of inside the click events
        Intent intent = new Intent(context, WebViewActivity.class);

button = (Button) findViewById(R.id.buttonUrl);
            button.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View arg0) {

                        // Set the url to google
                        intent.putExtra("url", "http://www.google.com");
                              startActivity(intent);
                  }
            });
            
            button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View arg0) {
                              // Set the url to yahoo      
                        intent.putExtra("url", "http://www.yahoo.com");
                        //Pass the intent
                                 startActivity(intent);
                  }
            });

             


//In the WebViewActivity class
 Bundle extras = getIntent().getExtras();
            if (extras != null){
                // get the URL
            String Url = extras.getString("url");
            
            if (Url == "http://www.google.com")
                {
                  webView.loadUrl("http://www.google.com");
                }

            if (Url == "http://www.yahoo.com")
             {
                webView.loadUrl("http://www.yahoo.com");
            }
        }
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now