-1

I'm getting a very mysterious bug in my app. On Android Studio, I'm checking for the value in SharedPreferences on the very first Activity (Launcher Activity), and if I get a value the app should go into background otherwise the Activity will be shown.

The problem is : When the app is first launched, it's going to background. But At the second time, It's showing the activity. Again, (for the third launch), the app is going to background. No debug points are working. Also no Logs are displaying. Please help me to get rid of this mysterious problem.

Thanks.

Edited:
 public class SecureAppsMainAct extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadSavedPreferences();
    }
    private void loadSavedPreferences() {
        SharedPreferences sp_lockcode = PreferenceManager
                .getDefaultSharedPreferences(this);
        String set_code = sp_lockcode.getString("LOCKCODE_SET", "");
        Log.e("set code", set_code + "");
        if (set_code.length() > 1) {
            //finish();
            Intent i = new Intent();
            i.setAction(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_HOME);
            this.startActivity(i);
        } else {
            Log.e("Load Prefs not working", "Load Prefs not working");
            Intent i = new Intent(SecureAppsMainAct.this, Main.class);
            startActivity(i);
        }
    }
}

The Manifest file:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity android:name="com.example.checkcurrentrunningapplication.SecureAppsMainAct"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.Dialog">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <activity
            android:name="com.example.checkcurrentrunningapplication.Main">
        </activity>

        <activity android:name="com.example.checkcurrentrunningapplication.LockDialog"></activity>

        <activity android:name="com.example.checkcurrentrunningapplication.UnlockingAct"
                  android:theme="@android:style/Theme.Dialog"></activity>

        <receiver android:name="com.example.checkcurrentrunningapplication.StartupReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="StartupReceiver_Manual_Start" />
            </intent-filter>
        </receiver>

        <receiver android:name = "com.example.checkcurrentrunningapplication.CheckRunningApplicationReceiver"/>

    </application>
Vivek Rathaur
  • 53
  • 2
  • 10

2 Answers2

0

I reformatted your code. You should use Boolean instead of String

public class SecureAppsMainAct extends Activity {
@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    loadSavedPreferences(); 
} 
private void loadSavedPreferences() { 
    SharedPreferences sp_lockcode = PreferenceManager
            .getDefaultSharedPreferences(this);
    boolean set_code = sp_lockcode.getBoolean("LOCKCODE_SET", true);
    //Log.e("set code", set_code + "");
    if (set_code) {
        //finish(); 
        Intent i = new Intent();
        i.setAction(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_HOME);
        this.startActivity(i);
        sp_lockcode.putBoolean("LOCKCODE_SET", false);
    } else { 
        Log.e("Load Prefs not working", "Load Prefs not working");
        Intent i = new Intent(SecureAppsMainAct.this, Main.class);
        startActivity(i);
    } 
}} 
zackygaurav
  • 4,369
  • 5
  • 26
  • 40
  • Sorry, but I just tried the way u say but to No Avail. SharedPreferences gets all the values stored in it (No matter it's String or Boolean) on the start of the app or at any moment. – Vivek Rathaur Feb 12 '16 at 11:15
0
Thank you very much all 3 who wanted to help me. 
I just Found that the problem occurred after every time the code of "minimizing the app runs". So, I just commented that code. I have no idea what's the problem with this code (or on Android Life Cycle): 
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);

but Anyhow I'll find an alternate way. I just Posted this so any other person might find it Useful.

Vivek Rathaur
  • 53
  • 2
  • 10