I do not know if this bug is in the code that I use to open the activity, or if it lies in the activity itself. This is the code for opening the activity, please tell me if I have an error in this, or if it is something else.
public void openGallery(View view){
Intent intent = new Intent(this, PhotoGallery.class);
startActivity(intent);
}
Related
I'm trying to open the MainActivity when the user clicks a button in my notification, while the app is only running in the background with a service. When the button is clicked, these lines are triggered in the Service class:
Intent openApp = new Intent(this, MainActivity.class);
openApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openApp);
I've checked it, and the lines are triggered, so there's no problem in reacting to the button's click, the Activity won't open though.
Any suggestions? Why isn't this working for me and how can I make it work?
Edit
I was asked for some more code, so in my onStartCommand() inside my Service, if it starts with a stop-action within its intent, I call the killService() method, which kills the Service, starts the MainActivity and do some other stuff:
if (action != null && action.equals(ACTION_STOP_SERVICE)) {
killService();
}
To set the Notifications button, I use this code:
Intent stopActionIntent = new Intent(this, TimerService.class);
stopActionIntent.setAction(ACTION_STOP_SERVICE);
PendingIntent stopActionPendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);
timerNotificationBuilder.addAction(R.drawable.stop, "Stop", stopActionPendingIntent);
And as I said, the button already reacts to the user clicking on it, so that's not the problem.
You can try to receive the click in a BroadcastReceiver and then open activity from there.
Try this to add a action button o your notification:
timerNotificationBuilder.addAction(createNotificationActionButton("STOP");
Where the createNotificationActionButton method is this:
public NotificationCompat.Action createNotificationActionButton(String text){
Intent intent = new Intent(this, StopwatchNotificationActionReceiver.class);
#SuppressLint("InlinedApi") PendingIntent pendingIntent = PendingIntent.getBroadcast(this, new Random().nextInt(100), intent, PendingIntent.FLAG_IMMUTABLE);
return new NotificationCompat.Action(0, text, pendingIntent);
}
Create a class named StopwatchNotificationActionReceiver and make it extent a BroadcastReceiver`. This is the code for that class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class StopwatchNotificationActionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
PrefUtil.setIsRunningInBackground(context, false);
PrefUtil.setTimerSecondsPassed(context, 0);
PrefUtil.setWasTimerRunning(context, false);
context.stopService(MainActivity.serviceIntent);
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
}
}
Also you need to register that receiver in your manifest like this:
<receiver android:name="StopwatchNotificationActionReceiver"/>
Where the MainActivity.serviceIntent is a public static variable which looks like this:
public static Intent serviceIntent;
And this intent is only used to start the service like this:
//In onCreate
serviceIntent = new Intent(this, TimerService.class);
//In onPause
PrefUtil.setTimerSecondsPassed(this,seconds);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
}
Or you can try the simple method:
if (action != null && action.equals(ACTION_STOP_SERVICE)) {
Context context = this;
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
killService();
}
Edit
Another solution is here. Again. You need to refer to my repo as I have made changes to the files in order to complete your task. In the service class, refer to this method. There, I start the activity if the action is reset(r). Or else, it opens the broadcast receiver. Then, in the activity, I receive that extra in the onResume() method. If the reset button is not clicked, it opens the Receiver class.
And as always, you can view the result of the app from here.
I hope that code will do your work.
I found it! See this answer.
This answer suggests enabling ScreeanOverlay settings because as of Android 10 and later you can no longer open an activity from the background just by calling the lines I've used.
To make it work, you'd have to add this permission through your Manifest.xml:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
And then the user would have to enable the Display over other apps setting.
I searched for an option to get the user to this setting more easily and found this answer.
This answer gives a code that redirects the user to the Display over other apps setting
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}
and then I guide the user with the notification's content (text) on how to enable the setting.
Once the setting is enabled, The lines I've used before work.\
So, problem solved?
Not Completely Solved
this whole configuration described above works, but only if the app is not killed.
If the app was killed and I try the method listed above, the app joins the recent apps list, but won't open and show up.
A solution that solves this issue as well will be accepted as an answer instead of this one.
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();
What I want is when I click on the button,start a service immediately.Once started the service,intent to another activity immediately.
Here is my code to achieve it:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here I start the service
Intent intent = new Intent(MyActivity.this, UploadService.class);
intent.putExtra(UploadService.ID,id);
startService(intent);
//after start the service,I want to redirect to Activity2.class
Intent intent1 = new Intent(MyActivity.this,Activity2.class);
startActivity(intent1);
}
}
But when I do this,the service is not start,and not redirect to Activity2.class,instead it redirect to the previous activity.
I tried intent inside the onHandleIntent(Intent intent) of Service.class the problem still the same
Here is what I tried in UploadService.class:
#Override
protected void onHandleIntent(Intent intent) {
//other code here
Intent intent1 = new Intent(MyActivity.this,Activity2.class);
startActivity(intent1);
}
But still the service not start,intent to the previous activity(the activity which MyActivity.class intent from).
So how can I redirect to another activity immediately just after start a Service?
Edit:
I already declare the UploadService in manifest like so:
<service
android:name=".services.UploadService"
android:enabled="true">
</service>
So what I still missing here??
Make sure you have registered your Service class in manifest.xml.
Try changing your Service class name from 'Service' to some other name. As Service.java class is System class.
Try starting service like below
startService(new Intent(context, MyService.class));
EDit
More Clarification Firstly check your app is running or not. If your Application is not running then call this code.
Intent i = new Intent(context,SecondActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
If running then use this code
Intent i = new Intent(context,SecondActivity.class);
context.startActivity(i);
This question already has answers here:
Is it a good idea to call finish() after starting a new Activity in Android?
(3 answers)
Closed 5 years ago.
protected void onCreate(Bundle savedInstanceState) {
//if user is already logged in open the profile activity directly
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, Home.class));
}
buttonSignIn.setOnClickListener(this);
buttonSignUp.setOnClickListener(this);
}
Can someone please explain to me why is finish() called before launching the Home.class if the user is already logged in. I'm trying to go through some source code and not able to understand this.
simply when you call finish();
its more like an intent which do this :
Intent intent = new Intent(whereYouareActivity.this , mainActivity.class};
startActivity(intent);
when you finish an activity, the activity gets destroyed but not the process. This is an important aspect of Android programming and is important to understand how it's different from other platforms.
And you can finish your activity by adding these lines :-
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}
After finish() you can't get context from the activity.
You have to start activity like this:
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}
By calling finish() you actively close your current Activity. This prevents your activity from being shown again when the user presses the back button.
I am making an android app which plays mp3 files. I am launching the mp3 playing activity from within another activity using intent:
Intent intent=new Intent(ListViewA.this,mp3player.class);
intent.putExtra("feed",gh);
i.setFlags(0);
i.setPackage(null);
//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now when the user selects another song from a list, I want to close the previous instance of the mp3player activity and start a new one using the intent code (above). How do I go about doing that? Thanks.
Instead of startActivity use startActivityForResult(intent, PLAY_SONG) then you can call finishActivity(PLAY_SONG) where PLAY_SONG is a constant number.
class member
private static final int PLAY_SONG
And then
finishActivity(PLAY_SONG);
Intent intent=new Intent(ListViewA.this,mp3player.class);
intent.putExtra("feed",gh);
i.setFlags(0);
i.setPackage(null);
startActivityForResult(intent, PLAY_SONG)
Hey its very simple Instead of calling Intent from Mp3Playeractivity call finish() when you are pressing the back button by implementing
#Override
public void onBackPressed(){
finish();
}
which will cause close your MpeplayerActivity