I have a registration form in android wherein the user can enter data from it. The user can search for Clinic. After that searching, the user submitted the record, and logout. If the user doesn't have no business with that app, the user can exit BUT when the user pressed the Exit button, it does not exiting, instead it goes to the Searching activity all over again even if the user is logout already. Can someone help me figure out why I can't finish my activity?
Search Activity
btn_Close.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent myIntent = new Intent(getApplicationContext(), RegForm.class);
myIntent.putExtra("from", "Search_Cancel");
startActivity(myIntent);
Search.this.finish();
}
});
Android Manifest
<activity
android:name=".Search"
android:noHistory="true" >
</activity>
Activities are finished with the this.finishActivity(Activity.RESULT_OK); call.
Note: You may also specify other results located in the Activity class as static finals.
finish() will make your active Activity pop from the Back stack but your others Activities will remain on that so if you want to exit whole app you should use Flag FLAG_ACTIVITY_CLEAR_TOP when you are starting your Last Activity to Clear your Back Stack
Related
I have an app that may run in the background. While it is running, it may bring a certain activity to the front. However, when the app brings the activity to the front, if the app is currently running at background, I do not want Android to bring the app itself to the front, just do it at background. The reason being is I do not want my app to interrupt what the customer is doing at the moment.
The following is my code that launches the activity.
Intent intentLogin = new Intent(getApplicationContext(), LoginActivity.class);
intentLogin.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentLogin);
The issue with the above code is every time when it is called, it always bring my app to the front even the app is running at background, that is, annoy my customer.
My question is, is there a way that I can quietly bring the activity to the front? If my app is running in front, thats great --- the customer see the new activity straight away. If my app is running at background, I want to quietly bring the activity to the front when app is running in background, and next time when my customer resume the app, they will see the new activity I brought forward.
How can I do that?
Thank you very much!
如果我没理解错的话:
App background and usual player.
When user logged out , you can clear the user data saved in local and show a notification.And when the notification is clicked, start LoginActivity.
App background and unusual player.
When the notification shows ,the user do not click it instead of run the app.You can check the user data whether existing or not by onResume()in BaseActivity.If not ,start LoginActivity.
App foreground.
clear user data and force to start LoginActivity.
If you don't like the solution above(the best user experence I think),try this:
In onCreate() in the specific Activity ,use moveTaskToBack(true) to hide the intent.But remember to check whether the app is running background or foreground.you can refer to this.
If understand correctly, I suggest this:
User leaves your app, and when user come back to your app you want to show another activity instead of .MainActivity
define another activity as Main and with NoDisplay theme.
<activity
android:name=".MainActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
then define your normal activities. In MainActivity class define a static variable. for example
public static boolean appRun = false;
onCreate() of SecondActivity change its value to true.
class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle Saved){
super.onCreate(Saved);
MainActivity.appRun = true;
}
}
and onPause set this.finish(); to avoid not resuming to SecondActivity and force to start from MainActivity.
then onCreate() or onResume() of MainActivity check it s value and navigate to another activity.
if(appRun == true){
/*it means user went to SecondActivity and you want to display another*/
startActivity(new Intent(context, NewActivity.class));
}
else{
//this is first time so navigate to SecondActivity
}
Also If you can use Fragments this solution can work for that too.
I have an app that it password protected to stop unauthorised access to data.
What I need to ask is:
when app is closed using the home button, then reopened I need it to go to the LoginActivity, I think I have sorted this using android:launchMode="singleTask"
But if I press the back button it takes me back to the menu without the need for logging in again - so it bypasses the password?
Can you please help?
On your activity, before it goes to background by pressing home, set a flag that the user has logged out or clear the session.
Then onResume of your activity, detect if the flag for user is still logged in or session is still valid. If not valid. then just send an intent to open your login activity.
So even you press back, that activity that resumed will validate if the user is still logged in or the session is still valid.
Create a custom application class with a flag indicating whether the user is logged in.
public class MyApplication extends Application {
boolean loggedIn;
}
In your activity you can then check if the user is logged in. If they aren't, return them to the login activity.
protected void onCreate(Bundle savedInstanceState) {
MyApplication app = ((MyApplication) getApplicationContext());
if (!app.loggedIn) {
Intent intent = new Intent(this, LoginActivity.class);
//go to old activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
I want some directions here. I have 3-Activities in an app i.e. MainActivity, MapViewActivity and DbActivity. I goes from one activity to another via. intents. The code of each intent is:
Intent intent = new Intent(this, ActivityName.class);
startActivity(intent);
I have used
android:noHistory="true"
in all these activities, so that when the mobile "end" button is pressed the app gets stop. Otherwise app was going back..... in spite of stoping the app
Now the problem is: When app goes for the GPS settings after finding GPS service off, the user turns on GPS and presses the mobile "end" button to come back to the app. But app also get stop. So user again start the app and comes on MapViewActivity. If I remove android:noHistory="true" from MapViewActivity, the problem also comes back. So is there any other solution for. Please guide me, if there is some method...
I don't think you mean "end". You probably mean "back". If you don't want the user to return to the activity when the user clicks "back", then just call finish() after you start the next activity. Like this:
Intent intent = new Intent(this, ActivityName.class);
startActivity(intent);
// finish this activity so that the user doesn't come back to it
finish();
Please take out the android:noHistory="true" from the manifest, this probably isn't what you want.
In my application I have a registration page, I want the user to fill her credentials and click a picture too. Once completed and the user is registered I want to prevent access to this particular page on press of back button. I had previously used
android:noHistory="true"
But when I start the camera intent with above line of code in my manifest declaration of the said activity I lose my data sent from the camera intent. I want suggestions as in if there is any other way to skip the page on press of the back button. Will overriding the back button be feasible?
Declare the Intent instance like this:
Intent showActivity = new Intent(CURRENT_ACTIVITY.this, TARGET_ACTIVITY.class);
showActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(showActivity);
finish();
The android:noHistory="true" attribute in the Manifest removes the trace of the Activity from the Activity Stack
EDIT:
You should use the above piece of code only after you have finished the Camera Intent code. That is, after the Registration Process is completed and you now want to, say, the Main Activity where the actual app will be in use. This code will naturally fail for the Camera Intent. ;-)
I have an application and every new created activity will start an async task to validate the user session. If the session is valid, the application flows continues. If not, the whole activity stack must be cleared and there should be only the login activity. This activity has a "no history" flag so it is never kept in the stack.
I've been trying some solutions provided here: Android: Clear Activity Stack but with no success.
This must works on the lowest android possible, being the least 2.2
Thanks!
I keep my login Activity on the stack. In the onResume() of the login Activity, I check to see if the user has login credentials and, if so, call startActivity for the next screen presented after login. The user does not see the login screen in this case.
When the user presses the logout button, I clear the user's credentials and then this clears the stack all the way back to the login screen:
Intent intentLaunchLogin = new Intent(this, ActivityLogin.class);
intentLaunchLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentLaunchLogin);
Also, if the user is on the screen presented after the login and they press the 'back' button, I don't want them to go to the login Activity. This code will send the user to the Home screen as would be expected:
moveTaskToBack(true);
Could you do something like is described here:
http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically
basically you create an alarm that starts your intent, then you close your app completely.
This is what I always do and works perfectly.
I start the app with the main activity an check if the user is logged in, if he is not logged in launch the login activity like this
void launchLoginActivity(){
/* Move user to LoginActivity, and remove the backstack */
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
It will not allow u to go back