i really hope, someone can point me into the right direction. I am currently developing an app for medical purpose. To secure their personal data, the user will get the ability, to create a password. Without that password there should be at least no access to the whole app. I implemented a simple enter dialog in my direct subclass of activity (which is inherited for all other activities).

But if the app starts another activity, the lifecycle will trigger the password dialog aswell. Even if the user just only saved his data.

How may i identify that the user comes from outside the app? I tried alot and it seems that the lifecycle stays the same if it just starts another activity or got sent to background and the user came back (which should trigger the dialog).

Thanks in advance

有帮助吗?

解决方案 2

Okay... i finally found a solution for this problem. At the end, inheriting the Application have been the right option. I am saving the last paused activity and if the same one gets resumed at any time, i display the dialog. Also i have to take care of configuration changes, which causes the activity to restart. If someone gets to the same problem as me, heres the code:

package at.***.**********.util;

import org.holoeverywhere.app.Application;

import android.app.Activity;
import android.content.ComponentCallbacks;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;

public class MyCustomApplication extends Application {
    private String lastActivity = "";
    private Boolean configChanged = false;

    public Boolean getConfigChanged() { return configChanged; }
    public void setConfigChanged(Boolean configChanged) { this.configChanged = configChanged; }

    private ActivityLifecycleCallbacks activityCallback = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityStopped(Activity activity) { }
        @Override
        public void onActivityStarted(Activity activity) { }
        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) { }
        @Override
        public void onActivityDestroyed(Activity activity) { }
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) { }

        @Override
        public void onActivityResumed(Activity activity) {
            if (activity.getClass().getName().equals(lastActivity) && !getConfigChanged()) {
                Log.v(activity.getClass().getName(), "Make activity show password dialog");
            }
            setConfigChanged(false);
        }

        @Override
        public void onActivityPaused(Activity activity) {
            lastActivity = activity.getClass().getName();
        }
    };

    private ComponentCallbacks componentCallback = new ComponentCallbacks() {
        @Override
        public void onLowMemory() { }
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            setConfigChanged(true);
        }
    };

    @Override
    public void onCreate() {
        registerActivityLifecycleCallbacks(activityCallback);
        registerComponentCallbacks(componentCallback);
        Log.v("Application startet", "Application startet");
        super.onCreate();
    }

    @Override
    public void onTerminate() {
        unregisterActivityLifecycleCallbacks(activityCallback);
        unregisterComponentCallbacks(componentCallback);
        super.onTerminate();
    }
}

if you ever have to restart your activity you may call:

((MyCustomApplication)getApplication).setconfigChanged(true);

Don't forget, to tell Android to use your custom application in manifest:

<application
    android:name="at.***.**********.util.MyCustomApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true" >

I'm sure there might be a better way but i didn't find one yet. Thanks alot guys!

其他提示

You can use SharedPreferences to write a flag indicating that you have logged in. Then you can clear that flag in your main activity's onDestroy or wherever you log off from your app.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top