?
Solved

switching views by button press?

Posted on 2013-01-22
2
Medium Priority
?
368 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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.
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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