Solved

switching views by button press?

Posted on 2013-01-22
2
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

735 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