Android launcher activity between registration Screen and default screen - android

I'm developing an application where a registration screen is required,
I initially put the registration sreen as the default
<activity android:name=".RegistrationActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And after successfully registered I should make the user never see this screen again
My work around was to make a new intent to the main actual screen
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
but what it does is that when I press back I see the registration screen again
Thanks

Try this:
startActivity(new Intent(getApplicationContext(), MyNewActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
this.finish();

Set android:noHistory="true" in the activity entry for your registration activity in AndroidManifest.xml. That will prevent the activity from being saved on the stack and it wont show up when you press back button.

What you do is make your main page your "LAUNCHER" activity. Then you check if the user is registered. If the user is not registered send them to the Registration Activity. When your user is finished registering finish the activity and refresh the main page when you return.
It's a good idea to keep the history of pages the app has been to, until you learn how to manage the application stack.

Related

I have to press back twice to exit my app

I have tried everything I have been able to find online to stop this happening before someone suggests 'Just go google it'....
I will be explicit in case it is not clear enough, I do not want to have to click back twice when I am on the registration or login screen. Its like I have an Activity A the user never sees, A starts an intent to either B or C.
On B or C I do not want to click twice to exit the application.
I have an Activity that I want to be invisible to the user, its purpose is to check if the application has been registered, if it has it starts an intent to login Activity, if the app is not registered it starts an intent to registration activity. The issue that when on the login or registration screen I have to press back twice to exit the application.
If I do not call finish() in this Activity the back button takes me to the transparent Activity and I have to press back again, calling finish() means I don't go back to the transparent Activity but I do have to press back twice - which I don't want.
I already tried calling the startActivityForResult() example, but this doesn't have any effect.
The Activity is using the transparent theme as using the NoDisplay theme caused an exception, googling this seemed to imply an issue with the emulator and suggested fix was to use transparent.
<activity
android:name=".activity.AppEntryPoint"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The Activity doesn't do a lot at the moment, currently I am trying the code below but this also has no effect and I still have to press the back button twice.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!"".equals(((GlobalData) this.getApplication()).password)) {
navigateToLoginScreen();
} else {
registerApplication();
}
}
private void registerApplication() {
Intent registerScreen = new Intent(this, RegistrationActivity.class);
registerScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(registerScreen);
finish();
}
private void navigateToLoginScreen() {
Intent loginScreen = new Intent(this, LoginActivity.class);
loginScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginScreen);
finish();
}
What can I do to make sure that the user only has to press back once when they get to the second screen, whether it is the login or the registration screen?
i think this solution will solve your problem.
just add android:noHistory="true" in the manifest file to your first activity, like this:
<activity
android:name=".activity.AppEntryPoint"
android:label="#string/app_name"
android:noHistory="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Home Button in Home Screen App android

I am working on app which will serve as Home Screen Lock. The app will support only 4.1 and above versions. The issue is handling the Home Button. I understand that we cannot override/disable Home Button, so it has to be handled a different way. I have been struggling for almost 2 weeks and tried many umpteenth ways but none of them working as expected. Main Activity is the one which will be on top and user need to enter his set password here to unlock the device. I have set my MainActivity as Home so the user is prompted to select between default launcher and my app. So expected action is to select my app as Launcher 'always'. Also I have set an flag in broadcast receiver whenever MainAcitivity is started. This flag is used in MainActivity to launch system Launcher in case MainActivity isn't started by broadcast receiver.
Everything is working fine till this point and approach. Issue is when I click on my app to change password or access other features, it doesn't open the app and opens the system launcher. The reason is it checks the flag in Main Activity and since it is not started by broadcast receiver, it doesn't launch MainActivity now and starts the system launcher. Below are code snippets:
Manifest File:
<activity
android:name=".MainActivity"
android:excludeFromRecents="true"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
My Broadcast Receiver
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_USER_PRESENT)) {
AppSession.getInstance().setReceiverCall(true);
MainActivity.isStarted = true;
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
context.startActivity(i);
}
Main Activity where I check the flag and start the system launcher. I amy doing this on onResume
if (!MainActivity.isStarted) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(mPackageName, mName));
startActivity(intent);
}
mPackageName & mName are system launcher name.
I tried other approaches as well but only one of the thing is resolved. Either home button is handled correctly and then my app isn't launched. If app is launched then on Home button press from anywhere it always brings my MainActivity on top.
I know this is doable as its done in many ScreenLock apps but somehow I am missing something. Please advise.

Open Activity with backwards to MainActivity in my app

I'm stuck with some activity's flow issue. The desired behaviour is the following:
From time to time, the user receives a notification. When this notification is clicked, a new Activity is opened with some information in it. In this Activity, there's a button whose purpose is to redirect the user to another Activity where more detailed information is showed. When the user is in the details Activity and presses the back button (or the back button in the ActionBar) this one is closed and the Main Activity is showed (this one is different from the one I mentioned in first place).
Everything works fine except from the last part. When the user presses the back button the application is closed and it is showed the Home Screen. Why is that happening?
Here is my AndroidManifest.xml:
<activity
android:name=".MainActivity">
</activity>
<activity
android:name=".DetailActivity"
android:label="#string/title_detail_activity"
android:parentActivityName="solar.panik.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="solar.panik.MainActivity" />
</activity>
<activity
android:name=".NotificationActivity"
android:theme="#style/NotificationActivity"
android:excludeFromRecents="true">
</activity>
Here is the onClick code for the button that starts the DetailActivity from the NotificationActivity:
Intent intent = new Intent(NotificationActivity.this, DetailActivity.class);
startActivity(intent);
finish();
Thanks in advance
When you start your app from something other than the launcher, you'll need to pass the back stack with your intent.
Android tutorial
Scroll down to Create Back Stack When Starting Activity().
So in your case:
// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
TaskStackBuilder.create(this)
// add all of DetailActivity's parents to the stack,
// followed by DetailsActivity itself
.addNextIntentWithParentStack(detailsIntent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
Check out this answer.
OLD ANSWER BELOW
Make sure in your details activity that onBackPressed() method isn't overridden (or defined).
If that's not it, try adding this to your manifest and remove your current ".MainActivity" Activity and tags. (Or replace it with this)
<activity
android:name="solar.panik.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to declare it as MAIN, so the Up button knows where to go. "Back" will take you to the next Activity up on the hierarchy.
Hope that helps.
Do this in your detailed activity. The one you click the back button in.
#Override
public void onBackPressed() {
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
startActivity(intent);
finish();
}

android back key on browser does not launch previous activity

I'm expecting quite a strange behavior in a tiny app I'm currently working on.
The app consists of two activities. From the first activity I'm launching the webbbrowser via an intent. When I press the back key in the browser it returns to the SECOND activity even if I manually closed the app previously before launching.
1) First Activity
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mWebShopURL));
startActivity(intent);
First Activity launches second activity like this
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
Bundle b = new Bundle();
b.putString("product", mProduct);
intent.putExtras(b);
startActivity(intent);
2) Second Activity -> first activity
onBackPressed();
AndroidManifest
<activity
android:name=".FirstActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
If you want the second activity to complete and go away when the user taps on the simple "back button" you refer to in your comment, then don't call onBackPressed(). Please just call finish(). That will make the second activity go away and return the user to the first activity.
Your problem is that you've never removed the second activity from the stack, which is why returning from the browser shows that activity.
If i am correct You are overriding onBackPressed() in your second Activity.Dont do that put onBackPressed() code in comments and try. Hope it will help :)

Android back button with browser oauth returns to browser

First time posting a question and I apologize, there are a ton of questions on this issue but after trying many different options I can't seem to get this to work.
I have a twitter application that the main screen has a button that requests oauth twitter access through the standard browser app, and then returns to the main screen. If I hit the back button, the browser app opens with the twitter website. I've tried noHistory in the manifest file, and when starting the intent FLAG_ACTIVITY_NO_HISTORY as well as FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP in many scenarios with no luck.
Here is my latest code:
Manifest file
<activity android:name=".oauth"
android:label="#string/oauth_name"
android:noHistory="true"
>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tm" android:host="twitt" />
</intent-filter>
</activity>
<activity android:name="com.tweetymanagerpro.Home"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
In the Home activity, I call the oauth as follows
Intent settingsActivity = new Intent(getBaseContext(), oauth.class);
settingsActivity.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(settingsActivity);
Next, in oauth.onCreate() I call for the twitter url
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
this.startActivity(intent);
Next, in oauth.onResume() I handle the tokens etc and then start Home again
Intent i = new Intent(this, Home.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
The connection to twitter works great, it's just now at Home if I hit the back button it takes me back to the browser where it is authenticating with twitter.
Consider rendering the oauth web page in a webview you control rather than launching out to the browser app.
The oauth web page, when it calls its callback, will actually be replacing itself in the webview, where your WebViewClient can catch that expected URL, process the results, and finish() the webview (removing it from the activity stack).
Is the oauth Activity the actual browser Activity? If so you can call finish() on it after you start the Home Activity in it to remove it from the activity stack.
If not, maybe if your Home Activity is started from your oauth Activity, your oauth class adds another flag in your intent. So if your home class sees this intent, it can override the BACK key press and not perform the default action (finish and resume to your browser Activity).

Categories

Resources