Is there any way to finish Activities automatically in Android Studio? - java

first I will give a brief explanation about my program.
I am trying to build a Point Of Sales System in Android Studio, Currently I have 4 Activities which are Login Activities, Register Activities, Forgot Password Activities, and Main Activities.
Basically when you use Finish() in each activities, it will dismiss all operation on the activity, in other words you can't go back to the previous activity after the Intent.
I am wondering instead to put Finish() on each activities, can I do that to all my activities automatically?

You can do this by keeping no histroy of back stack. Try below code.
Intent i = new Intent("your intent stuff");
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i)
or second way of doing the same You can do this by adding flag in your manifest as well. Add below code in your manifest file.
android:noHistory="true"
You need to add above tag in application tag.
Hope this will help you.

Just add flags into the intent before starting activity
in Kotlin
val intent = Intent(this, NewActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
in java
startActivity(new Intent(this, NewActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));

You can add this.And it's clear all previous activity and data.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Related

How to properly close login page?

For example, first activity of app is MainActiviy which is basically 2 text fields (login and password) with a button. Pressing a button tells the database these text fields' data so it can tell the program if the user is in database and password is correct.
What I'm used to is to create a bool flag field saved in SharedPreference so the program knows i already logged in my app recently so it can open my second activity, but it seems like wrong and weird solution.
So, how do I properly login into my app in theory?
Mikkel,
Your approach with storing data using shared preferences is perfect but I have some suggestions which can make this solution better and robust.
First of all, you should check the sharePreference value inside a splash screen. Which checks for the login flag and launch the LoginActivity if a user is not logged in and launch the HomeActivity if a user is logged in.
Second, I would suggest using Androidx DataStore, which is a better version of shared preference and it can be asynchronous API so you can use it safely on the UI thread.
Don't forget to close your main activity after user login using the below code.
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If you need an example to handle this stuff better here is the github-sample which can be very helpful.
This is wrong:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
You should use "startActivity" before "finish()"
Correctly:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

Is it possible to relaunch an activity from another activity after the finish() method has been called on it?

Is it possible to relaunch an activity A from activity B after the finish() method has been called from activity A?
how can I go about it in order to achieve this?
Intent intent = new Intent(this, Activityb.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use this piece of code and just change the activity name accordingly. This will finish your acitivty A and redirect to activity B

Pressing back button on phone has different behaviour to back intent

My app is tabbed, when I start a new activity over the top of the tabs, and press the back button on the phone to return, it returns to the tab I was previously on. When I go back with this intent:
Intent intent = new Intent(EditViewerActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
It doesn't return to the tab I was on before. Is there anyway to get the same behaviour as the back button on the phone in an intent?
Solution was to use finish();.
finish(); will end the current activity, the proper way would be to use NavUtils.navigateUpTo(this, new Intent(this, MyActivity.class)); which can be used after importing import android.support.v4.app.NavUtils;
They will both basically do the same thing in the end.

Android activity stack control. Can't figure out how to remove history

I have following 2 activities(plus many more not important for this question):
<activity android:name=".activities.HomeActivity" android:excludeFromRecents="true" />
<activity android:name=".activities.AdHocActivity" android:noHistory="true"/>
HomeActivity is a first one and only one wich I keep history for. User can go differnt places from Home and click Back to come back to main HomeActivity.
I also have service running on alarm and checking for some specific things. When specific criteria met I'm displaying my AdHocActivity
Intent i = new Intent(context, AdHocActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(BlockingActivity.INTENT_BLOCKED_PACKAGE_NAME, packageName);
context.startActivity(i);
FLAG_ACTIVITY_NEW_TASK was necessary to show Activity from BroadcastReceiver
Now, this AdHoc activity displays some message to user and has a button to take user back to HomeActivity
private void sendToMainApplication()
{
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
finish();
}
All this work, but I get second instance of HomeActivity in a stack. So now when user taps "Back" - my Home activity flashes and comes back (previous copy).
I want only one copy to stay on top. I want it to be like anchor one. If it doesn't exist - I want new one to be opened.
The trick to prevent HomeActivity from being doubled was to call it like this:
private void sendToMainApplication()
{
Intent i = new Intent(this, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}

How do I create an android shortcut to an activity?

OK,I'm new at this forum, so don't blame me for putting this in the wrong tags,not putting something in,eg.
I want to learn how to create a shortcut(I did that by Googling) and link it to an activity (In this case, com.android.mms.ui.ComposeMessageActivity)
I tried doing it, but it only showed me a toast saying "Application not installed" and I'm pretty sure it is.
It would be better if you can display a "complete action with another application" dialog.
If I assumed your question correctly, you mean a button or something within an activity that leads to another activity, that being -- "com.android.mms.ui.ComposeMessageActivity"
if your activity that you want to link to is in another application-- then
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.mine", "com.android.mms.ui.ComposeMessageActivity"));
startActivity(intent);
if it is within the same application, then
Intent intent = new Intent(this, ComposeMessageActivity.class);
startActivity(intent);
//optional add this to your manifest to finish the current loading activity so
//as to not keep it in the activity stack
//<activity android:name="yourActivity" android:noHistory="true" ... />
EDIT If you mean a shortcut on a homescreen, then I would create a tiny application that only has one activity which uses the above method to link to a different application. Then I would drag that application to the home screen, and boom. If there's a better way, then please feel free to correct me

Categories