I have developed a custom launcher for android. The launcher is working fine except for a 'bug' that it has.
I have given the user certain shortcuts on the main screen for accessibility.
Now suppose the user starts the Youtube activity. When the user presses the back button it goes back to the launcher, which is fine.
I am invoking the activity like this
PackageManager manager = context.getPackageManager();
context.startActivity(manager.getLaunchIntentForPackage(youtubeactivitystring));
I have also provided a shortcut to a browser activity for help on the main screen which is invoked as follows:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://url/content/help"));
context.startActivity(browserIntent);
When the user clicks on HELP he is unable to get out of the browser (it's stuck there for some reason). When the user clicks the HOME button, the user lands on the launcher. When the user goes back to start another activity (say youtube again) and then get out the activity by clicking the BACK button, somehow the browser activity opens. I am unable to understand why is this happening.
Please help
Related
I have an application which has a main activity. When it switches to a second activity I do it like this :
startActivity(intent);
finish();
I was hoping this would fully prevent the application from going back to the main activity. When I press the android back button in the secnond activity it goes back to the home screen (which is what I want). But when I open the app again from multitasking it goes back to the first activity! Has anyone run into this issue before?
I was running into this problem, where my first screen would be a login activity that's only meant to show if the user is not logged in. Otherwise, it show the home screen. I tried out SharedPreferences for login persistence, but it turns out the authentication service I'm using, Google Firebase, has built-in login persistence.
Without more information, however, I can only suggest that you research SharedPreferences and have a boolean check a condition upon start-up. If the condition is met, start the home activity; otherwise, show the main activity. Something like:
boolean isLogin;
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);
isLogin = settings.getBoolean("islogin", false);
if(isLogin) {
finish();
goToDash();
}
Public Interface Shared Preferences, Android Developers
Obtaining Shared Preferences
Working with Android Shared Preferences
Figured it out! My application uses a twitter api, and relaunching the application would cause an error as it retried to get credentials it didn't have before. I also made sure to completely clear the stack when switching certain applications by doing:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
Hey guys I'm currently developing an application for my final project, the concept is a contact keeping app but with user login and registration. When the user enters their credentials and presses the login button it starts an Intent to move to another page, on this Intent I end it with the .finish() method so the user can't go back but when that is executed and lets say by accident the user presses the physical back button on the device the application will close and if you try to open it again by going on the multitask physical button on the device and you select it, it starts the application again from the beginning (the login screen) how can i make it that if the users presses it by accident they can open it again from the multitask or the physical icon of the application so the it picks up on where it left at (the display activity after you login it) essentially not restarting the application.
Is that even possible?
Thanks in advance
Override onBackPressed() in your activity and remove super.onBackPressed().
#Override
public void onBackPressed() {
// super.onBackPressed();
}
First of all to stop device default back key event you have to remove super.onBackPressed() from onBackPressed() of activity
#Override
public void onBackPressed() {
// super.onBackPressed();
}
Now to keep the flow management after login, you have to save the activity name or any other value to SharedPreferences so whenever you come again to the application you will make a check for last activity after login. And navigate to the same.
SharedPreferences preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE);
Editor editor = preferences.edit();
//add this to onCreate of the activity where you want to come directly
editor = editor.putString("LAST_ACTIVITY", "MULTITASK_ACTIVITY/*Navigated activity name*/").commit();
Now make a check on app start in loading screen:
if(preferences.getString("LAST_ACTIVITY", "").equalsIgnorCase("MULTITASK_ACTIVITY")){
//Navigate to Mutitask activity
}else{
//Navigate to other activity
}
I have login page (activity) which is my first activity (landing page) of the app.
Only after user gives correct username and password, the app allow you go to other pages to work with.
Now my question is, now the user is at the fourth page and he is working on it, And he minimize the app and use other app like messaging etc..
so when he come back to click my app icon again, my app starts from the first landing page, which is the login page,
how can I make my app to resume from the fourth page?
Android 4.4.4, Kit Kat, Samsung
Regards,
Nay TK
Add this in android manifest of loginActivity,
android:launchMode="singleTask"
more on android:launchMode
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
go through official link for more detail
You can make your fourth activity as your launch activity.
In the Resume of your Activity, you check if the user is logged in. If not, you start your Login activity.
I have a flow in my application like this:
For new users:
Splash Screen --> Login Activity --> Home Activity
For already registered users:
Splash Screen --> Home Activity
Basically the Splash Screen has an if else to decide which activity to go to. Once a first time user logs in, his status is saved in a preference variable for the splash screen to decide next time not to open the login activity.
Now the situation is that. If a new user logs in and goes to the home activity, and then logs out. He is redirected to the Login screen which is pretty much what should happen. But, in case an existing user opens the app, he is shown the Splash screen and directly moved to the Home Activity. Now if the user logs out, he gets out of the app. This happens because the Login Activity does not have any instance created and thus finishing the Home Activity finishes the whole app. Logout actually finishes the Home Activity, naturally the last active activity should open up. Which is not happening.
What I want to do is that, I want to implement a logic which will check that the Login Activity is available or not. If its available then finish() will be called else the Login Activity will be called via intent.
Please tell me how to achieve this.
P.S: My app uses a custom theme with a customized action bar. If I call finish and Intent together or I use flags to clear existing activities then there is a weird transition effect which shows the black standard action bar for a split second thus creating a bad user experience.
Now if the user logs out, he gets out of the app. This happens because
the Login Activity does not have any instance created and thus
finishing the Home Activity finishes the whole app.
If i understood your question, why dont you just call the Login Activity manually after user click a logout button?
Its what i always did with apps that have flow like yours
when user login finish login activity and start home activity.
when user logout finish home activity and start login activity
You always can call Login Activity via intent. If the activity is available, android will show this activity. Else android will create new activity automatically.
Actually that's why we use intents to show activity instead of creating activityes manually. System catches this intents and does all dirty work.
EDIT:
Hmm, but wouldn't you have the transition problem anyways? (If you were already logged in, and then log out - using intent/finish() you will have the same black action bar issue no?)
Maybe consider following ( I actually did this in my app):
Merge Splash screen and Login into one activity and depending whether you are logged in - display the login fields or proceed to home screen. Then you have a out of box consistent stack of activities regardless of use cases and no mambo-jumbo with do I already have this or not.
I can't comment because of I lack 4 rep, so I'll post as answer here:
I think #Blaze Tama is right. You can also use FLAG_ACTIVITY_CLEAR_TOP on intent to avoid stack flow problems:
From docs:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
Always start Login activity and start Home activity right away if the user already logged in.
In the Splash Screen activity
Intent intent = new Intent(this, Login.class);
If (user already logged in)
{
intent.putextra("Logged in", true);
}
startActivity(intent);
In the Login activity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null)
{
if (intent.getBooleanExtra("Logged in", false))
{
startActivityForResult(new Intent(this, Home.class), requestCode);
}
}
else
{
// The existing code here
}
}
In Home activity send back a code to indicate if the user logged out or just BackPress. If BackPress finish this Login activity.
Here is my problem. I am developing app that loads some documents from server. I open document in another app via Intent.ACTION_VIEW. This is all working just fine. Problem is that whole app is pin protected so I have to capture events such "sent to background" or "screen lock" to bring up pin screen afterwards and this is not working when another app is opened above mine. So if user opens document then press home button, click on my launch icon from menu then he gets again external app with opened document and with back button access my app again. This is security issue that needs to be fixed.
Here are some code snippets:
Opening document:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivityForResult(intent, 1);
capture sent to background:
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
IN_BACKGROUND = true;
Log.i(PinUtil.class.getSimpleName(), "App sent to background ");
} else {
IN_BACKGROUND = false;
}
}
My question is: Is it possible to detect if my app is sent to background when another app is opened? How not to open another app when my launcher icon is pressed.
Thanks for all responses.
Regards
Lubos
In order to fix this problem:
So if user opens document then press home button, click on my launch
icon from menu then he gets again external app with opened document
and with back button access my app again. This is security issue that
needs to be fixed. Here are some code snippets:
You need to make sure that, when you launch an external app for the user to view a document, that the external app does not run in the same task as your application. It needs to run in a new, separate task. You can do this like this:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Ensure app runs in separate task
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivity(intent); // Can't use startActivityForResult() here
However, you can't use startActivityForResult() when you launch the external viewer, because an activity running in another task cannot return a result to you. However, most external applications won't return a result when launched with ACTION_VIEW anyway, so it probably isn't a problem.
Then you asked this:
My question is: Is it possible to detect if my app is sent to
background when another app is opened? How not to open another app
when my launcher icon is pressed.
There should be some answers on StackOverflow that can help you determine if your application is in the background (it isn't actually that easy to determine this).
My explanation above should answer your 2nd question. If you don't launch other apps in your task, then only your app will be launched when your launcher icon is pressed.