Open Intent in a new context - android

When I open a link inside the Facebook app, it opens a new task for the browser while I can switch back to the Facebook app instead of opening the browser inside itself like an ordinary Activity. But when I launch an Intent to my application's Google Play page it behaves like an Activity inside my app instead of launching a separate instance for Google Play.
Here's how I'm starting the Intent:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=MY.APP.PACKAGE")));
I found this thread and wrote this snippet:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=MY.APP.PACKAGE"));
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Which fails with this message in logcat:
04-29 12:35:42.681: E/AndroidRuntime(8482): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.LAUNCHER] dat=market://details?id=MY.APP.PACKAGE flg=0x50000000 }
Any ideas?

You just need to remove the addCategory() call and it should work.

Related

How to clear previously opened activity in Android

Intent intent = new Intent(MainActivity.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Opening coupon activity to apply coupon:
Intent intent = new Intent(Payment.this, Coupon.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From coupon activity opening payment activity with updated data
Intent intent = new Intent(Coupon.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The problem is, when I click on the back button on payment activity after applying the coupon, it opens the previously opened payment activity again (not the main activity).
onBackPressed() I don't want to do it static (like Intent intent = new Intent(Payment.this, MainActivity.class); and also don't want to use finish(); in payment activity.
Please help.
You can use FLAG_ACTIVITY_NO_HISTORY when launching the Payment activity.
See: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NO_HISTORY
This is a Simple Use case of startActivityForResult don't make it complicated with Intent Flags and launch modes.
Whenever you Open your Activity Coupon just use startActivityForResult and upon applying coupon send the result back to previous Activity (success/failure) or whatever result you want . Then you can change the UI as per the result you got in onActivityResult() .
You can follow How to manage startActivityForResult on Android or the latest way from the doc Getting a result from an activity .

Installed app doesn't open intent

I have an instant app with two features with differents urls.
Launching the example.com/A from the android studio this code works nice, the activity B (or feature B) is opened.
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://example.com/id/"+mIds.get(mPager.getCurrentItem())));
intent.setPackage(getPackageName());
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);
But running the installed app, when I press the same button I get the error:
android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE]
dat=https://example.com/... pkg=com.example.myapp }
Any idea?

Launch Chrome from Android app but point to local content

So, from my app I can launch Chrome and get it to load a page using the http:// using the code below.
My problem is that when I try and load content that is stored locally on the device. i.e. file:///, I get the error further down.
Any ideas on how to work around this?
Code:
String urlString="file:///storage/emulated/0/Download/primer.html";
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
startActivity(intent);
Error:
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=android.intent.action.VIEW
dat=file:///storage/emulated/0/Download/cuescript_primer.html
flg=0x10000000 pkg=com.android.chrome }

Failed to go back to home screen from Android apps

I have a button in my app named "Go Home" to redirect the user to the home screen. It is working fine without the very first launch. The process of first launch is noted below:
After uploading the APK into SVN I am downloading using the web browser. Then go back to the download folder and installing the app. When install finishes I click on Open. Then In my app I click on the "Go Home" button. The application redirect me to the web browser instead of the home screen. I am tired to search a solution for that.
I am using the following code:
finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Thanks in advance, Siddiqui Noor
Your app is opening in the task of the browser. Try this:
finish();
Intent intent = new Intent(context, HomeActivity.class);
intent.setAction(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
try adding FLAG_ACTIVITY_NEW_TASK:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and also try putting finish() after startActivity
I'm pretty sure that it's not really redirecting you anywhere, it's just closing the Activity. You call finish() after which the intent to start the activity never happens. The app is closed because you've finished the Activity and you end up looking at the screen that was showing before you opened the app. In this case, that is the browser.
Try removing the line to finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
kill your current activity after redirect the next activity
finish();

How to go to the default home screen of Android programatically?

In my app, I have a button called EXIT, when the user clicks that, I want to finish all the activities of my app, which are in the stack, and go to the default home activity or the all apps activity.
I have written the following code in my onClick():
Intent intent = new Intent(Intent.CATEGORY_HOME);
startActivity(intent);
But it gives me the following error in logcat:
03-12 11:22:18.279: ERROR/AndroidRuntime(308): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.category.HOME }
So what do I need to do to achieve this? Do I need some configuration in the manifest or is my approach wrong?
Try this:
Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
use following code to launch home screen:
Intent intent=new Intent(this, HomeClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Categories

Resources