How i can close all activities of my android app in background - java

I created an android app and i want to start my app at the MainActivity every time my app is launched? That is if my app is at activity B and the user press the home key, then when the app is launched again, it starts at MainActivity instead of activity B? how i can do that ?

There is a flag for exactly this purpose. In the <activity> declaration for your MainActivity add:
android:clearTaskOnLaunch="true"
This will cause the app to start at MainActivity whenever it is relaunched.

There are few ways to do this -
In your activity B override the 'onPause' method and add 'finish()'
In your manifest file where you have added <activity> tag for your activity B, you can add 'android:noHistory=true'. Check this link to understand more - https://developer.android.com/guide/topics/manifest/activity-element
I hope this will solve your issue.

Related

Finishing an Activity with transition shows the previous Activity from the activity stack first and then transitions to the new Activity

I have 2 Activities in my Android application - Activity A and Activity B.
When the user presses a button on Activity A, he is navigated to Activity B.
I want Activity A to close after going to the next activity and is completely removed from the activity stack.
I tried adding intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); , but this not work as Activity A was still accessible after pressing the back button.
I added finishAffinity(); , this worked, but while transitioning, the previous activity in the activity stacks gets visible and then it goes to Activity B.
Video
(here the application drawer is shown, and then it goes to Activity B) -
Code -
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this);
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivity(i, options.toBundle());
finishAffinity();
Any fix so that the previous activity is not visible and activity is also closed ?
EDIT : I tried finish(), finishAfterTransition() and supportFinishAfterTransition() instead of finishAffinity() but still the previous screen is visible.
So I guess there's an issue with finishAfterTransition()
I solved it by adding android:noHistory="true" attribute to the activity (which has to be removed from the stack) in the manifest file solves the issue.
There is no need to use finish() in the java code after this change.
Method 2 -
Refer #Pratyay ' s answer for a way around.
you have to use finish but like this:
startActivity(Your Activity here)
then Finish()
if you do reverse you can see black screen for a short time do this way
#Override
public void onBackPressed() {
finishAffinity();
}
Use this function in Activity B, this will exit app on pressing back button. And don't use any finish(); or finishAffinity(); in Activity A. This will give a smooth transition

How to put a MapsActivity inside another activity

I created a "Google Maps Activity" (G) and "Navigation Drawer Activity" (N) from the wizard of Android Studio, when I use like Main Activity (Launch) the G activity, this show a mark (from the java source sample created on onMapReady method). But when a use the N activity like the main activity, and I copy the content from the "activity_maps.xml" inside "content_main.xml" to put the map inside the G activity, this don't show the mark. I guess the Java source for MapActivity of the G activity doesn't execute.
This is the directory tree:
This is the XML files:
And the Java code (I don't change nothing):
I tried to change in MapsAcivity "setContentView(R.layout.activity_maps);" by "setContentView(R.layout.activity_main);" thinking the activity_maps is copied inside content_main that is inside activity_main. Also, change in content_main "tools:context="com.example.ws02.myapplication.MapsActivity"" by "tools:context="com.example.ws02.myapplication.MainActivity"" because this work in the main_acivity.

Android studio change startup activity only once

I am developing an Android app with a Navigation Drawer working this way: the user opens the app and a Tutorial (an activity with a pager sliding fragments, called ScreenSlideActivity.java) pops up; when the user finishes sliding the tutorial, he presses the "finish" button resulting in the initialization of the MainActivity (creating a drawerLayout, drawerToggle etc.)
What I need to do is opening the Tutorial activity just once, after the app's first launch.
I've tried with this code in the main Activity:
if (savedInstanceState == null) {
SelectItem("tutorial");
}
making sure that ScreenSlideActivity.java is immediately started. The problem with this solution is that when the tutorial is opened, from there I'm not able to access MainActivity.java anymore, neither from the "up" botton neither from the Tutorial's last page's "finish" button, probably because for some reason I don't have the main as the parent activity anymore.
Then I've tried this solution change application's starting activity - Android modifying the manifest xml file. Adding:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
to ScreenSlideActivity.
The problem with this solution is that it changes the structure of my project, making my ScreenSlideActivity.java as the Main and Launching Activity (and therefore from here I cannot access the MainActivity anymore) while all I want to do is to display it once.
What else can I do?
You can use SharedPreferences to check/set if it's the first time opening the app
String preferences_name = "isFirstTime";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
firstTime();
}
...
public void firstTime(){
SharedPreferences sharedTime = getSharedPreferences(preferences_name,0);
if (sharedTime.getBoolean("firstTime",true))
{
//Your tutorial code
sharedTime.edit().putBoolean("firstTime",false).apply();
}
else
{
//When not using tutorial code
}
}
Similiar question here : https://stackoverflow.com/a/13237848/4555911
What I usually to deal with temporary screens is similar to your first solution:
In your MainActivity check if it is the first time the user launches the application. The most common way to do this is using SharedPreferences and checking if a boolean is true or false.
If it is the first time, you start a new activity for your ScreenSlideActivity class with no particular flags. your MainActivity will be in the stack:
MainsActivity -> ScreenSlideActivity
When the users press the finish button you can call the finish() method on your ScreenSlideActivity which will remove the activity from the stack and bring back your MainActivity.

Start two activities from Android application

I have an application A that wants to start an Activity in another application, B, which I don't own and cannot edit.
If B is already running and visible in recent apps, there's no problem in executing the wanted Activity of B using an Intent.
If B isn't running, I use the following code to execute its main Activity first, and then the one I want to execute:
String bPackage = "com.example.applicationb";
PackageManager pm = getPackageManager(this);
Intent main = pm.getLaunchIntentForPackage(bPackage);
Intent wanted = new Intent();
wanted.setPackage(bPackage);
wanted.setComponent(new ComponentName(bPackage,bPackage+".WantedActivity"));
main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
wanted.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
wanted.setExtras(mPreviouslyCreatedBundle);
startActivity(main);
startActivity(wanted);
The wanted Activity executes, but after some seconds I get an error and it stops working. Am I setting the Intents in a wrong way?
Make sure u have setted exported="true" for activity you are trying to redirect to another package
Basically my idea is when your second paackage app leave u need :
android.os.process.killprocess(android.os.process.mypid())
and when you launch use flag as start new task :
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Android How to get Last Focused Activity after "Force Stop" the Application

I am developing android Application having number of activities. The requirement of the application is When i "FORCESTOP" the application manually(Settings->Applications->ManageApplication->APP_NAME->FORCESTOP), the last focused activity(UI Page) to be displayed. Can i get the Last focused Activity after *FORCESTOP*ing the Application? Please help me with the sample code.
You should keep track of each activity change and save it to file (e.g. in your sharedpreferences) immediately when the change happens. Something like this
protected void onResume() {
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).edit();
edit.putString("LastActiveActivity", this.getClass().getName());
edit.commit();
super.onResume();
}
Make sure you save the current activity in the onResume() method of all your activities.
When you restart you can read the last activity back from your preferences. This should be done in the onCreate() method of your main activity (the one that is specified as "LAUNCHER" in your manifest). Then start the activity with the name you've read back.

Categories