What I originally had: a launcher activity with a button which would launch my main activity. The launcher activity had shared preferences so it would only show up once. However, I have added a new activity that when the user clicks the button on the launcher activity, a introduction page is to appear. However due to shared preferences on my launcher activity, that introduction page does not show up.
So is there any way to clear the user's data on update? Just for this update, When a user updates my app, I want this to run:
SharedPreferences settings = getActivity().getSharedPreferences("cda-preferences", Context.MODE_PRIVATE);
settings.edit().clear().commit();
Is this possible?
Yes, you can define a BroadcastReceiver for the MY_PACKAGE_REPLACED action. In this receiver's onReceive method you should clear the flag in the shared Preferences. (API12+)
<receiver
android:name=".OnUpgradeReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
Or you should save the version of your app (to the shared prefs). When the user launches the app you should check what was the previous one and if its newer, show the introduction page.
Related
I want to build Terms and Conditions dialog display on a "startActivity". If the user clicked on the Accept button or clicked on a Checkbox, it start the app with MainActivity. But later, on second/third etc. start, i will skip the "startActivity", i will run the app direkt with the MainActivity. (if the user clicked earlier on the Accept button). How can this be solved?
Thanks!
You just need to:
Make sure your main activity is the one system starts
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In your Main activity check if user has already accepted terms or no. If no, drive him to TnC activity.
Obviously state of TnC acceptance must be stored somewhere (preferences, room, firebase etc).
Use SharedPreferences to store and access the user's response. https://developer.android.com/training/data-storage/shared-preferences#java
SharedPreferences sharedPref =
getActivity().getPreferences(Context.MODE_PRIVATE);
String res=sharedPref.getString("UserResponse","NONE");
if(res.equals("Acccept")){
//goto main activity here
}
//get user response into String response
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("UserResponse", response);
editor.commit();
Consider 3 activities:
(A) Login Activity (main launcher)
(B) Main Activity
(C) Specific Activity
After users logged in (A) they will access (B), and from, and ONLY from (B), they can access (C).
Now everytime the app is opened, it will first launch (A) and then (B).
I want to make a push notification where when clicked, it can access (C) but must start (B) first.
I am using Xamarin.Android + Appcenter Push Notification
I can get notifications both when my app is in foreground and background.
My problem is when my app is in the background, clicking at the received notifications in the status bar causes it to relaunch the app, starting from (A).
I need help with skipping (A) since user is already logged in, opens (B) and THEN opens (C)
Any ideas? Hoping this is not too confusing for you guys.
I have also tried setting launchMode=singleInstance to (A) and (B) but it still relaunch the app
You cannot change the launcher activity dynamically, but there are a few workarounds you can try:
Create a transparent activity and make it as the Launcher activity:
<activity
android:name=".ActivityLauncher"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And select the next activity in it's onCreate()
if ( logged() ) {
intent = new Intent(this,ActivityB.class);
} else {
intent = new Intent(this,ActivityA.class);
}
startActivity(intent);
finish();
You can achieve this send a activity name or some value to select your activity. Depending on the value that u are sending you can launch the activity and also add the activity in backstack whenever u go back fron notified activity the page in backstack will appear.
I do always like this, hope this will work for you.
I'm developing an Android app which requires the user to be logged in (actually, at the moment they just need to supply a username as no authentication is required... this will change in the future though).
I've managed to implement this, but I'm not sure if it's the best way. I have a LoginActivity which I have declared in the manifest using android:noHistory="true" and:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In LoginActivity's onCreate method, I check for a Boolean value isFirstStart stored in SharedPreferences. If the value does not exist, it must be the first start of the app, so I let LoginActivity set everything up, perform the login, and then launch MainActivity using:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(MainActivity.EXTRA_ISFIRSTSTART, isFirstStart);
startActivity(intent);
I use the extra so that I can open the navigation drawer in my main activity if it is the first start. At this stage I also store the Boolean value as false in SharedPreferences.
If isFirstStart already exists as false, I perform the launch of MainActivity straight away using the same method.
To me, this somehow doesn't seem like the most efficient method - launching LoginActivity on every startup even though it will not be used most of the time.
Any thoughts?
I have a dictionary-type application that accepts, via an intent filter, arbitrary text shared from other apps, e. g. the browser. The intent filter is attached to a decicated activity ("Lookup") that does some posttranslation of the shared text and invokes my main activity. The lookup activity quietly closes.
I'm testing it with the system browser on Samsung Galaxy with Android 4.0.4, and here's a funny thing I notice. On the first try, it works as expected. I tap "Share", select my app as the target, the lookup activity is invoked, then the main activity is invoked. Now, if I task-switch back to the browser and hit "Share" again on a different piece of text, the menu of sharing targets is not displayed. Instead, my main activity is resumed. Not the lookup one (that'd be reasonable), but the main one. onNewIntent() is not delivered to neither main activity nor Lookup activity.
onResume() is delivered to main, though.
However, if I tap "Back" instead of task-switching back to the browser - that is, explicitly close my main activity - on the next try, the Share operation displays the menu of targets again.
What's up with this behavior? Does the Share functionality remember the target of the last share operation and try to reuse it? If so, why won't it remember the immediate target of the share - my lookup activity? Is this documented?
EDIT: the manifest for the lookup activity goes like this:
<activity android:name=".Lookup"
android:label="#string/IDS_LOOKUPTITLE"
android:theme="#style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
When I start main activity from lookup activity, I'm trying to reuse an existing one (if any), so the flags are FLAG_ACTIVITY_REORDER_TO_FRONT|FLAG_ACTIVITY_SINGLE_TOP.
In my application, I have specified a second activity that can be launched from the launcher, using this manifest entry:
<activity
android:name=".Lists.ListOfListsActivity"
android:icon="#drawable/ic_launcher_lists"
android:launchMode="singleTop"
android:label="#string/lists_activity_name" >
<!-- An Intent filter so that the Lists activity shows in the Launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Let's say I have the app open at the "main" activity then press the home key. My app will still be running, but in background.
Later the user selects the launcher icon I have for "ListsOfListsActivity" from the homescreen.
This will bring the application to the foreground, but NOT at the "ListOfListsActivity", but at whatever it's current activity was when it went to the background (e.g. at the "main"activity).
This is confusing, as the user selected the "ListOfListsActivity" but is shown another one. Then they have to navigate to it.
I had this working better, by specifying launchMode = "singleTask" for the "ListOfListsActivity", but in that mode it cannot be launched from another activity for a result (startActivityForResult() ), and I need to be able to do that to pick a list...
Question:
- how to specify an intent-filter that will force an activity to the foreground and be the selected activity, no matter what the current status of the application and it's current activity??
My final implementation was to define a different taskAfinity string for each activity I wanted to launch independently from the Launcher.
That way, each "shortcut" always launches the activity I want, but the downside that I have not been able to avoid is that the user may have multiple tasks with an activity from my application in it, and maybe the same activity open/active in different tasks....