How to always start from a startup activity on Android?

后端 未结 9 1317
走了就别回头了
走了就别回头了 2020-12-09 10:52

There are three different cases:

1) A user launches an app, navigates in it, pressed home and click on the app icon again to launch our app again.

2) A user

相关标签:
9条回答
  • 2020-12-09 11:29

    It seems a similar question has already been asked. It sounds like the OP came up with a working solution. How do I collapse "child activities"?

    EDIT: Instead of using a button you can use a boolean to tell whether or not you need to collapse back to the main activity. Have your root activity extend from Activity and the child activities extend from CollapsableActivity. To get this to work in all cases I added startOutsideActivity() and startOutsideActivityForResult().

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    
    public class CollapsableActivity extends Activity {
        private boolean returnToRoot;
        public static final int COLLAPSE_BACK = -1; // something other than RESULT_CANEL (0)
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            returnToRoot = true;
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            returnToRoot = true;
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            // start collapsing the stack
            if (returnToRoot) {
                setResult(COLLAPSE_BACK);
    
                finish();
            }
        }
    
        @Override
        public void startActivityForResult(Intent intent, int requestCode) {
            super.startActivityForResult(intent, requestCode);
            returnToRoot = false;
        }
    
        public void startOutsideActivityForResult(Intent intent, int requestCode) {
            super.startActivityForResult(intent, requestCode);
            returnToRoot = true;
        }
    
        @Override
        public void startActivity(Intent intent) {
            // call startActivityForResult to make sure and catch the collapse condition
            super.startActivityForResult(intent, 0);   
            returnToRoot = false;
        }
    
        public void startOutsideActivity(Intent intent) {
            super.startActivity(intent);    
            returnToRoot = true;
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (resultCode == COLLAPSE_BACK) {
                returnToRoot = true;
            }
       }
    }
    

    This worked properly for me in all cases you listed. The only difference is you need to call startOutsideActivity() or startOutsideActivityForResult() when you navigate away from you app. Personally, I think this adds clarity to your intentions. Hope it helps!

    0 讨论(0)
  • 2020-12-09 11:30

    You can try this steps:

    1. use one boolean static flag isFinish in StartupActivity with default false value.
    2. in onCreate() of StartupActivity set isFinish value to false.
    3. write below code in onResume() method of all activities in your project.

      if(isFinish)
      {
         finish();
      }
      
    4. set isFinish value to true when you open any native app like email, browser etc.

    or

    5 . set isFinish value to true in onBackPress() method whenever you want to close application on back press.

    Case 6: if android browser open on clicking on any link then use below code is onPause() method

    if(isBrowserRunning("com.android.browser"))
    {
       isFinish = true;
        finish();
    }
    

    ////////////////

     private boolean isBrowserRunning(String processName)
            {
                ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
                String packageName = manager.getRunningTasks(1).get(0).topActivity.getPackageName();
                Log.i("LogTest", "Current process package name: " + packageName);
    
                return processName.equalsIgnoreCase(packageName); 
            }
    

    You can create a sample project to know other browser package name like opera mini, US browser etc.

    add below permission in manifest:

    <uses-permission 
            android:name="android.permission.GET_TASKS" />
    
    0 讨论(0)
  • 2020-12-09 11:35

    This should be handled on the Application level.

    For API level 14, you can register an ActivityLifeCycleCallback in your Application class

    public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)
    

    You can use it, to know on an Application level, which activities are destroyed, paused, resumed etc etc. Whenever, an activity is paused, without a new activity being created/resumed, you should clear the Activity stack, and re-launch your startActivity

    If you target SDK versions < 14, you should implement your own method, to know which activities are created/resumed and paused, and do the same whenever an activity is paused, without a new activity being created/resumed

    0 讨论(0)
  • 2020-12-09 11:36

    You need to use bundle and pass appropriate parameter/or parameters from the calling app (i.e. click something in the app (TextView with a link)).

    Retrieve the parameter in the called app (Email app).

    You can send the name of the activity in the parameter.

    Now being in Email app(the called app) Click of back button navigate back to your calling application.

    Optionally you can save the state of activity from the caller program, as required.

    You need to use Bundle, and Intent to implement this logic.

    Code snippet:

    In the calling program, we need to store parameters/data required for back button functionality in the called program.

    Bundle bndleData = new Bundle(); Use putString(), putInt() methods of Bundle class.

        String prefix = getPackageName().toString();
               (this prefix can be stored in application level constants.java file as applicable)
    
        bndleData.putString("ParentActivity", this.getLocalClassName());
    

    Also store additional parameters if required bndleData.putString("paramName", valueofParamName); bndleData.putInt("IntChannelImage", chImageInt);

        Intent intent = new Intent(v.getContext(), AMRChannelPlayer.class);
    
        intent.putExtra(prefix + "bndleChnlData", bndleData);
    
        startActivity(intent);
    

    Caller Program: Retrive the data, activity nae from bundle and use it in back button implementation:

    prefix = getPackageName().toString(); Bundle extras = getIntent().getBundleExtra(prefix + "bndleData");

    String parentActivity = extras.getString("ParentActivity"); extras.getString("paramName");

    I hope this helps you.

    0 讨论(0)
  • 2020-12-09 11:40

    Instead of using multiple solutions you can use a single one that solves all the problems.

    Check this answer:

    https://stackoverflow.com/a/8576529/327011

    With a Broadcast and BroadcastReceivers in each activities of your application you can kill all activities whenever your application goes to background.

    UPDATE:

    To detect if your application when to background you can use onStop, check this to understand the theory: Activity side-by-side lifecycle

    And this is the implementation: https://stackoverflow.com/a/5862048/327011

    I think this is all you need :-)

    0 讨论(0)
  • 2020-12-09 11:50

    I know you don't want to manage it in all activities but you can do this and still handle the code in one place with a super activity

    public abstract class BlundellActivity extends Activity {
         @Override
         public void onPause(){
             // Whatever strategy you want
         }
    }
    
    public class SomeActivity extends BlundellActivity {
         // Do whatever you normally want to do
    }
    
    public class SomeActivity extends BlundellActivity {
         // Do whatever you normally want to do here as well
    }
    
    0 讨论(0)
提交回复
热议问题