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
Solved

switching views by button press?

Posted on 2013-01-22
2
345 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

837 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