Prevent an Android app from 'minimizing'

Asked

Viewed 1,204 times

2

I don’t know if minimizing would be the right word, I don’t think so... Please correct me.

I have an application and I want to stop someone from minimizing it using the Home button, if someone presses Home, the application will be completed completely, including all the Activities that have been opened, and I also need you to execute a method when this application is finished in these terms...

How to do?

Edit: It is an application that user can select files to protect against access from other applications, these protected files can be accessed only by my application (it asks login), while my application is running the files are unprotected, because it needs to 'unprotect' them in order to be able to read, many laymen click the Home button and think that the application has been finalized, I want to avoid this, I want to avoid that the files remain unprotected because the application is still running, and I want to prevent another person from taking the user’s phone and end up gaining access to the application and files because it is simply open

  • If you really need to do this, allow me to suggest that you should rethink the design of your application, because this goes against the way of programming for android.

  • yes i need to do this, it is an application that protects some files, these protected files can be accessed only by the application, and the application asks LOGIN, yes I need the application to be killed in this case

  • Most lay people don’t close the application, simply click the Home button, my application needs maximum safety. I have just received a negative vote because of this

  • @ramaral edited the post mentioning why this. I’ve seen my competitors doing the same, I just wanted to know how, if there is another way is also good

  • The correct way to do this is to control the call Activity Lifecycle, in times of Stopping and Restarting an Activity

1 answer

2


Android is particularly designed to do pause to an application when the home is used.

Because of this, there are two methods to manage what should happen when someone uses the home or navigate out of the application: onPause() and onResume().

You can learn more about how they work in Pausing and Resuming an Activity.

By way of example, you can make a class of your own that extends to Activity in order to accomplish certain tasks when the application pause:

public class BaseActivity extends Activity {
    @Override
    protected void onPause() {
        // raio, usaram o botão 'home', deixa-me proteger os ficheiros!
    }

    @Override
    protected void onResume() {
        // epá, utilizador voltou, deixa desproteger os ficheiro!
    }
}

public class Activity1 extends BaseActivity {
    // ...
}

Also for your case, it may be useful to define android:clearTaskOnlaunch="true" so that the activities are completed when someone returns to the application.


Check whether application in background

It may also be useful to know if the app is going to background, which allows us to react more strongly.

The function below makes it possible to check if the application is going to background. It can be called in the method onPause() of all the Activity of the application:

 /**
   * Verifica se o aplicativo está sendo enviado para segundo plano
   * (ou seja, para trás da atividade de outra aplicação).       * 
   *
   * @param context O contexto
   * @return <code>true</code> se outro aplicativo vai estar acima deste.
   */
  public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
      ComponentName topActivity = tasks.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        return true;
      }
    }

    return false;
  }

The function needs extra permissions so you should in your AndroidManifest.xml include the following:

<uses-permission android:name="android.permission.GET_TASKS" />

Credits from this function to the @peceps user on SOEN in this answer.

  • I had thought about it, but how would I do in case of Login?

  • Because the application manages sensitive information, I think that the ideal is to finish the login and force it to start and/or re-start the application. But this is a consideration to have in the design of the application and what is intended for it.

  • in case it didn’t work.. onPause is called with dialogs too, and onStop is called after it calls other Activity

  • there is no way to check if the application is in the background?

  • There is, the method onPause() is called when the application goes to background.

  • I added a function to the answer that checks the question of background.

  • according to the Android doc, this is to be used only for Debug, I think I should have problems with the play store if using this

  • there should be some onBackground on android, ta doing very much miss here... If I use onPause and onResume is not right, because they are affected when there are changes of Activity, I do not know what my competitor did, but when I press the home button in his app, the app is simply killed and does not even appear in that list of cache Android 4.0+ (when we hold the home)

  • 1

    The method getRunningTasks(int maxNum), was depreciated in API level 21 (LOLLIPOP), for security reasons, for the possibility of leaking personal information to third-party applications. (If using this method be aware that it will not be supported for future Android versions).

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.