In my Broadcaster Receiver,
I have this set:
//Intent that enables the view to pop up when alarm goes off
Intent i = new Intent();
i.setClassName("com.example.Device.MainActivity", "com.example.Device.OtherActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
and in my notification service, I have this:
// First let's define the intent to trigger when notification is selected
// Start out by creating a normal intent (in this case to open an activity)
intent = new Intent(this, OtherActivity.class);
For some reason, when the notification goes off, the activity that I want to pop up does not pop up when alarm rings.
How can I pop up my activity when the alarm goes off/rings? I have done it before but I changed something in my code to mess it up.
Related
I have developed an android apps that have a splash screen and a login page. The problem is when a user give credential and pressed log in button it open controlActivity. but when I pressed back button, I navigate to login page again If I pressed back button again I navigate to splash screen. How can I stop navigating to previous page, when user press back button then app should exit?
Why does it happen ?
This is happening because whenever you open another activity from intent, it creates it over it.
How to solve the problem?
You can add a flag like this
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
OR
You can finish it.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
If you want to close the app onBackPressed, you can override the method. Add this to your activity.
#Override
public void onBackPressed(){
finish();
System.exit(0);
}
Doing this, the app will be closed on back pressed!
You need to put a flag in the activity you want to be forgotten.
Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Answer by #Sambhav Khandelwal should solve your problem. In your splash screen you need to do add flags with intent like:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class); //assuming your splash screen name as SplashScreenActivity and login as LoginActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Or you can do without flags like below:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
startActivity(intent);
finish();
Sorry if title is not clear, here's an in-depth explanation:
I'm in the Settings activity of my app and I have an option to change the app's theme. Obviously I want the entire theme of the app to change immediately when I change this option. So I'm using OnSharedPreferenceChangeListener in this way:
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("theme_preference")) {
Intent refresh = new Intent(getApplicationContext(), SettingsActivity.class);
refresh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); //clears previous activities
startActivity(refresh);
}
}
As you can see I added flags that would clear the previous activities so that they'll be recreated too to have their themes updated.
The Settings activity recreates itself just fine, but then when I press the Up or Back button to go back to the previous activity the app crashes. Why is it crashing?
This is how you start a stack of activities:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
stackBuilder.addNextIntentWithParentStack(intent);
Intent nextIntent = ...;
stackBuilder.addNextIntent(nextIntent);
stackBuilder.startActivities();
I have this Activity1(Restaurant) which switch to Acticity2(Home)
Intent intent = new Intent(RestaurantActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
My other Activity (Home) has a boolean to check if I have been at Home before
public static boolean visitedHomeAlready = true;
if(!visitedHomeAlready) {
Intent intent = new Intent(HomeActivity.this, RestaurantActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); //
}
My idea is: If I have not visited the home before and switch to Restaurant Activity, I want to "remember" the data in the Home Activity to continue my game after come back home but it always start from the beginning from my home...
I am not sure which flags I have to set or which method to resume that state from where exactly where I switch the activity.
As Amin commented above,the finish(); means that the first activity has finished its job and the app will end it,if you removed it the second activity will start,but the first activity will still be in its place and you can go back to it just by clicking the back button on your phone or virtual device.
I want to start activity from notification. I want to open an activity, which is successor of some other activities.
Example activities: IntroActivity -> Photos -> SpecificPhoto. What I want to achieve: In case user clicks on notification, I want to open SpecificPhoto activity. Keep in mind, that app can be running (for example PhotosActivity is displayed), or it can be shut down.
I want to preserve back button functionality (move to PhotosActivity on back pressed).
On notification click, I need to launch IntroActivity, because user needs to login here in case he is not.
I tried following (using constants in activities, code):
On PhotosActivity onCreate:
redirectToActivity();
RedirectToActivity method:
private void redirectToActivity() {
Intent intent = getIntent();
int activityCode = intent.getIntExtra("code", 0);
switch (activityCode) {
case SpecificPhotoActivity.CODE:
startActivity(new Intent(this, SpecificPhotoActivity.class));
break;
default:
return;
}
}
By applying this approach, I can traverse the whole activity stack and go to the activity I want. However, this approach is not working in every case. Sometimes, the activity_code is not set (don't know why) and therefore we end in the first activity.
Is there any more professional approach to solve this issue? I believe this must be solved somehow in many apps.
What you want is called TaskStackBuilder.
Here's how you should construct the intent, that would navigate to SpecificPhotoActivity:
Intent action = new Intent(context, SpecificPhotoActivity.class);
PendingIntent pendingIntent = TaskStackBuilder.create(context)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
In order to correctly specify stack of activities, you should provide android:parentActivityName inside manifest file:
<application ...>
<activity android:name=".SpecificPhotoActivity"
android:parentActivityName=".PhotosActivity"/>
</application>
With this parameter you have specified, that the parent of SpecificPhotoActivity is PhotoActivity, thus TaskStackBuilder would understand where to navigate as soon as back button is clicked inside SpecificPhotoActivity.
Construction of the notification should be as follows:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(...)
.setContentText(...)
.setSmallIcon(...)
.setContentIntent(pendingIntent)
.build();
manager.notify(NOTIFICATION_ID, notification);
Now notification click would open SpecificPhotoActivity. A click on back button would navigate to PhotosActivity.
What's left is authorization handling. I suppose you are able to apprehend whether user is authorized or no during the construction of the notification. Hence, following approach should work:
PendingIntent pendingIntent = null;
if (authorized) {
Intent action = new Intent(context, SpecificPhotoActivity.class);
pendingIntent = TaskStackBuilder.create(context)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
Intent action = new Intent(context, IntroActivity.class);
action.putExtra("photos_flow", true);
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
}
Now, inside IntroActivity after successful authorization:
void onAuthorized() {
if(getIntent().getBooleanExtra("photos_flow", false)) {
// most possibly you should pass some id into SpecificPhotoActivity's intent
Intent[] intents = new Intent[]{new Intent(this, PhotosActivity.class), new Intent(this, SpecificPhotoActivity.class)};
startActivities(intents);
finish();
}
}
I've got some activities which I execute one after another without closing (1->2->3->4). I want to close 1,2,3 activities from 4 and execute a new one; the code:
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
UserpickActivity - 4th activity, CommunicationActivity - the next activity. But after this code all previous activities still worked. How can I close all running activities and execute some new activity?
Try this:
When you want to launch CommunicationActivity from Activity4, do this instead:
Intent intent = new Intent(this, Activity1.class); // Your root activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will remove all other activities from the task
intent.putExtra("foo", true); // This is to tell Activity1 to launch Activity4
startActivity(intent);
In Activity1.onCreate() do the following:
super.onCreate();
if (getIntent().hasExtra("foo")) {
// We should now launch CommunicationActivity and finish ourselves
Intent intent = new Intent(this, CommunicationActivity.class);
startActivity(intent);
finish();
return; // Don't continue the rest of onCreate()
}
// The rest of your onCreate() goes here
You can only use CLEAR_TOP to clear down to an existing activity in the activity stack. That's why your use of CLEAR_TOP isn't working (because CommunicationActivity doesn't exist in the activity stack).
This method clears all activities and launches the root activity again. When the root activity's onCreate() is called, it checks the Intent for the extra "foo", which it uses as a signal that it needs to launch CommunicationActivity and finish itself. This should do what you want.
Call activity.finish(); after you switch to other activity. This way no activity will remain on stack.
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//YOUR_ACTIVITY.finish();
Uncomment the above line if you want this activity to be removed from activity stack.
// This will clear all activity and start new task.
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use android:nohistory=true in manifest for Activities.
<activity
android:name=".A"
android:label="#string/app_name"
android:noHistory="true" >