Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

switching views by button press?

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

610 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