Asked  7 Months ago    Answers:  5   Viewed   20 times

I have two different activities. The first launches the second one. In the second activity, I call System.exit(0) in order to force the application to close, but the first activity is automatically displayed instead of the application returning to the home screen. How can I avoid this, and get the application to return to the home screen?

 Answers

11

You should really think about not exiting the application. This is not how Android apps usually work.

Tuesday, June 1, 2021
 
rlanvin
answered 7 Months ago
13

FIGURED OUT THE ROOT CAUSE :-)

I figured out the reason why it wasn't working for me. I am using a different 3rd party launcher (other than Stock android launcher) on my phone. CREATING and REMOVING an App-Shortcut works as long as the launcher you are using supports that operation. I ran the above code on default launcher and it works like a charm :)

Thank you everyone for your replies!

Thursday, July 29, 2021
 
WooDzu
answered 4 Months ago
11

If your app hasn't been "terminated" then #1 should already work and #2 just requires saving any values that aren't managed automagically into the Bundle in onSaveInstanceState() then restoring them in onRestoreInstanceState().

This is kind of a hack, but I think your best option for #1 in the case of the app actually being terminated would be to save the most recent Activity in the onResume of each of your Activity classes then when you first run the onCreate of your first activity do a check then start the correct Activity... maybe even put in a blank Activity at the beginning. Something like this:

StartActivity:

public class StartActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get last open Activity
        String lastActivity = PreferenceManager.getDefaultSharedPreferences(this).getString("last_activity", "");
        if (last_activity == MyActivity2.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity2.class));
        } else if (last_activity == MyActivity3.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity3.class));
        } else {
            // assume default activity
            startActivityForResult(new Intent(this, MyActivity1.class));
        }
    }

    public void onActivityResult() {
        // kill the activity if they go "back" to here
        finish();
    }
}

Then in all the other Activities (MyActivity1,2,3) save the values like so:

@Override
public void onResume() {
    Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit();
    e.putString("last_activity", getClass().getSimpleName());
    e.commit();

    super.onResume();
}

You'll also have to handle saving /restoring the data for each Activity manually. You could save all the values you need into the preferences inside the onPause() of each of the Activities then restore it in the onResume().

Thursday, July 29, 2021
 
toesslab
answered 4 Months ago
31

When constructing your class, you can have a constructor that takes in a Context and assigns it a local Context object within your class.

public class LocationFinder {
     private Context myContext;
     private Geocoder geocoder;

     public LocationFinder(Context context)
     {
         myContext = context;
         geocoder = new Geocoder(myContext);
     }

}

And then when you try to access this class, make sure you initialise it like:

public class TestActivity extends Activity {
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          LocationFinder lFinder = new LocationFinder(getApplication());
     }
}

Of course, you can't access a context from every class that you will be running. So a reference to a View can suffice.

LocationFinder lFinder = new LocationFinder(anyView.getApplication());
Sunday, September 26, 2021
 
tuckerjt07
answered 2 Months ago
39

Solved. The deeplink class was starting an intent for each deeplink and these required that I set the flags NEW_TASK and CLEAR_TASK for each one. After that - everything worked.

Sunday, November 7, 2021
 
Lak
answered 3 Weeks ago
Lak
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share