Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID: Facebook and Browser Open inside my app, not in their own

this will be a bit annoying to explain, but bear with me:

So i have a simple android app with buttons, one opens a facebook page (first checks if the app is installed, and will open in that, else will open same page in browser), the other opens a website url in the browser

The problem i'm having is that, instead of opening them as seperate applications, it seems to be opening the browser/fb app inside my original app.

So if i open the fb page through my app, and click the back button, it brings me back to my app's home screen. If i click the fb button, and minimize the app, and go into the ACTUAL fb app, it hasn't changed anything there.

So my app is not actually using the standalone fb app, just calling it up inside itself.

(hope that makes sense...) here is the code i used;

    //just defined button variables
    button facebook, shop;

    //now for the onCreate method

    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    facebook = (Button) findViewById(R.id.facebook);
    shop = (Button) findViewById(R.id.shop);

    facebook.setOnClickListener(new View.onClickListener(){


    public void onClick(View v) {
    try{                                   //left out the profile id on purpose...
        Intent facebook = new Intent(Intent.ACTON_VIEW, Uri.parse("fb://profile/xxxx"));
    startActivity(facebook);

    }catch(Exception e){
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.facebook.com/example")));
    }
    }
    });

The shop button is set up the same, only without the try/catch, it will only open in the browser.

How do i make it so that it sends the request to the actual fb app/browser, instead of what it's currently doing? Any help/tips are gratefully welcome :)

like image 272
Chris S Avatar asked Oct 21 '22 20:10

Chris S


1 Answers

Create a new layout with layout/a.xml

Open the layout file & then add a WebView, assign an ID so you can access the widget later.

   <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:orientation="vertical" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"> 

          <WebView android:id="@+id/webview" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"/>

     </LinearLayout>

Insert Permission in your manifest file as you are accessing INTERNET in your application

     <uses-permission android:name="android.permission.INTERNET" />

Inside the new Activity -->WebActivity.java

     public class WebActivity extends Activity
     {

      public void onCreate(Bundle savedInstanceState)
      { 
     super.onCreate(savedInstanceState);
     WebView mywebview = (WebView) findViewById(R.id.webview);
     WebSettings webSettings = mywebview.getSettings();
     webSettings.setJavaScriptEnabled(true); 
     mywebview.loadUrl("http://www.facebook.com");
       } 
     }

If you want to handle navigation inside the webView on your own

    mywebview.setWebViewClient(new WebViewClient()) 
    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return false;
        }

Now to redirect from your main activity to these new activity ::

    facebook.setOnClickListener(new View.onClickListener(){


public void onClick(View v) {
    try{
    ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
    boolean isFacebookInstalled = true;
        }
   catch( PackageManager.NameNotFoundException e ){
                    isFacebookInstalled=false;
              }

  if(isFacebookInstalled)
   {
       //start the facebook app
        Intent intent = new Intent("android.intent.category.LAUNCHER");
        intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
        startActivity(intent);
   }
 else
   {
    Intent facebook = new Intent(getApplicationContext(),WebActivity.class);
    startActivity(facebook);
   }
});

Same way you can also insert for your shop button

like image 198
Vikalp Patel Avatar answered Oct 29 '22 18:10

Vikalp Patel