I created an app to simply open a web browser and go to a specific website. However, after it's run for the first time, if I go to another app and run the app again, I will get nothing except the black screen. I knew I can find the page from web browser, however I wonder if there is a way to bring the content back when I launch the app and the user could get back to work from where he/she stopped.
here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** setup the url and start a browser with
* the hotmial.com and return to the user
* */
String url = "http://www.hotmail.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
Thanks guys
Take a look at the activity lifecycle. You may want to move your intent to OnStart() for when the application comes back to the foreground after being stopped.
The activity may still exist when you run your app a second time; perhaps onCreate is not called.
After you throw the Intent to run the browser, shut down the activity so that onCreate happens every time.
If your application is here only to give a link to a website, this is much overhead in my opinion. Android users can make a shortcut to any website they want on the desktop, the shortcut icon being shown with the site's favicon in its corner.
Try this if you want to open the browser OUTSIDE your app. I know it seems like a simple(and trivial) edit, but I've never had issues setting it up this way.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** setup the url and start a browser with
* the hotmial.com and return to the user
* */
String url = "http://www.hotmail.com";
Uri uri = Uri.parse(url);
Intent i= new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
}
Why don't you use the android.webkit.WebView which will allow you to directly browse web pages in your app. Here the web content will remain persistent as long as the application is running. On the chance that your application stops you could save the url using WebView.getURL() which would allow you to continue from where you left of the next time you start your app. If this is what you want to do let me know and I will post some sample code.
Related
After setting up an app for deep linking. by creating an activity to launch when the link is opened in the browser. How do I go about getting the link that the browser used to start the intent?
Let us say the app catches the URL: "https://example.com/foo/bar?param=1&data=extra".
In the Activity launched I want to use the URL which opened the app. I am thinking maybe something like int the activity perform
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
I dont know the variable to access the lauch URL from Uri data
It turns out I was on the right track all I do is
data.getQuery();
To get the last part of "https://example.com/foo/bar?param=1&data=extra" ie "param=1&data=extra"
Otherwise, call another function of data like.function() to get other aspects of the URI eg the whole URL that led to the opening of the activity
Stackoverflow makes you think half the time I always find the answer Immediately after I post a question.
I have a very simple activity, that redirects the user to the app's Play Store page, when the button is clicked:
public class MyActivity extends AppCompatActivity {
private static final String PLAY_STORE_URI =
"market://details?id=" + BuildConfig.APPLICATION_ID;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
findViewById(R.id.go_to_play_store).setOnClickListener(this::goToPlayStore);
}
public void goToPlayStore(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(PLAY_STORE_URI));
startActivity(intent);
}
}
Is it possible to write a test to check that the PlayStore is launched when the button is clicked? Better, is it possible to verify it shows the expected page?
I know that by using ActivityMonitors transitions between Activities can be tested. I also know that I can verify the Intents being sent using Espresso Intents. But can I actually check that a user action launches another app?
I would click the button, then use:
intended(allOf(
hasAction(Intent.ACTION_VIEW),
hasData("https://play.google.com/store/apps/...your app...")
))
I would suggest a slightly different question - is it you app's job to verify that? You'd be testing not your own app but Android OS and Google's Play Store App.
The way I've been approaching it is by being explicit about the boundaries, and aware of the possible scenarios.
What I mean by that is, extract the Intent manipulation and invocation logic into a thin service (that's the being explicit about boundaries part) and test your Activity correctly interacts with it (by constructing the Intent appropriately).
The part about possible scenarios is being aware of what can happen. For example what does your app do (if anything) if the user doesn't have Play Store on their phone.
I'm building an app which has an Intro page with many slides. Once a first time user has gone through the intro, he'll be directed to a login screen. Once he logged in (or registered), he'll be taken into the app home page. As long as the user doesn't sign out, if he clicks on the app icon he'll be directly taken to the home screen.
I'm using the Intro page intent as the LAUNCHER activity and using sharedpreference to save 'first usage' and logged in states. By testing if the user has logged in or a first time user, I'm directing him to different intents.
So my question is, where is the most suitable position to have this intent redirection? Because Intro page has so many fragments and components, setting it as the LAUNCHER activity and having all the if else statements there to decide where the user should go, have I wasted system resources? Because if the user has already logged in, he'll taken into the home page without showing any app intro stuff which are loaded.
Or is it a good practice to create an empty activity and set that as the LAUNCHER activity and put all the if else statements in that. So the app doesn't need to go to the 'heavy' app intro page.
PS: I've declared those intent direction if else statements in the onCreate right after super.onCreate();
#Override
protected void onCreate(Bundle savedInstanceState) {
// Fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
// activity_first_usage is the container for all frames
setContentView(R.layout.activity_first_usage);
logger = new Logger(this);
if (!logger.isFirstUsage()) {
if (logger.hasTOKEN()) {
// If user didn't log out, then he can stay in the app
Intent home = new Intent(getApplicationContext(), Home.class);
startActivity(home);
finish();
} else {
// If this is not the first time user login in, no need to show the intro
Intent directToSignIn = new Intent(getApplicationContext(), SignIn.class);
startActivity(directToSignIn);
finish();
}
} else {
// If not, continue with the Intro and set usage status to used
logger.setFirstUsage(false);
}
...
}
ill tell you the concept
use a splash and there use a condition to check user's state eg: already registered , new one , registered but still did not go through intro like wise
identify it
now you use shared preff
can write a file
can keep a enum value
or get from a server etc..
more : you can think about what happens when user uninstall your app and re install it.Then what you need to do ? up to you.
once you identify the state of the user in the splash
the write different intents to each of them
if a new one - display your intro
if not - load to your main menu
you need to decide cuz you knows the requirement
Hope this helps a bit :)
In the starting activity of my app I show a dialog to user and ask if he wants to see some contents in my website or not.
If he clicks No, the dialog disappears and I call continueActivity() to do some process and go from current activity to MainActivity.
If he click yes, I want to open the webpage in the external browser and again I call continueActivity() to do some process and go from current activity to MainActivity.
The problem is in positive state. This is my code in positive state:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaa.ir"));
startActivity(browserIntent);
continueActivity();
Because of calling continueActivity(), the external browser can't open and if I don't call continueActivity(), the URL opens in the external browser and the app sticks in the current activity.
So how could I open the URL and at the same time, continue the process and go to other activities.
I usually use
startActivityForResult(browserIntent, BROWSER_REQUEST);
and override onActivityResult(int reqCode, int resultStat, Intent intent)
if(reqCode == BROWSER_REQUEST) {
continueActivity();
}
Once you call startActivity, your current Activity will follow through the exit of the Activity lifecycle. This is unavoidable.
If you have processing you want to continue in the meantime, you should consider a Service. If you want the App to return to its current state, you need to store relevant data and load your Activity to its previous state (or next intended state).
in both ways ContinueActivity is opening. i don't know you architecture. You could just add to intent some extra like
intent.putBoolean("openExternalLink", dialogResultHere);
Then in continueActivity you will got this intent like
getIntent().getBoolean("openExternalLink")
also dont forget to delete this option, otherwise you will open browser each time after Activity recreation (screen rotation, minimize, etc.)
getIntent().removeBoolean("openExternalLink");
P.S. signatures of methods could be littlebit different, but general idea is here
I have added intent-filter to my activity, so I can receive send intents that contain text, I have set data to text/plain, everything works fine, I can choose my application from the picker, it is opening, showing me the text, but, when I minimalize my app, and then click again on ColorNote application (I am using it to take my notes) it is opening my app instead of ColorNote, anyone ever had similiar issue? And know how to resolve it?
#Edit
Do not know why I am getting - points, if you think it is easy question you could write how to solve it instead of giving -.
By the way, I don't think any code is neccessary in here right now.
That's the way it should behave. For e.g. if an application starts another and you leave the application without closing it's task you will return to the intent it started. I can reproduce this with apps which take me to the play store:
What I did:
First test:
Start application
Let it start the Play Store
Press Home
Start the application again
welcome back to the app store
Second test:
Start application
Let it start the Play Store
Close application (Task Manager)
Start the application again
welcome back to the application
Okay now on how to solve your problem. I would check if my application was started by another intent -> http://developer.android.com/training/basics/intents/filters.html
boolean wasStartedByOtherApp = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (intent.getType().equals("text/plain")) {
wasStartedByOtherApp = true;
}
}
#Override
protected void onPause() {
super.onPause();
if(wasStartedByOtherApp){
finish();
}
}
I have not tested this. This should just give you an idea on how to solve the issue you are facing. But be warned as an android user I would be angry if an application closes itself just when I want to check my calendar if I have time and would like to return to the intent the app just started.