0

I am still learning Android development and I am stuck at a point where I assume I am doing something wrong. Would appreciate your help.

I have my main class that extends AppCompatActivity like this, and inside it, I have a function that instantiates another class where I want to do some calculations based on the store sharedpreferences:

public class Level1_0 extends AppCompatActivity {
.....
public void isTwoUnlocked(){
        CalculateAvg calc = new CalculateAvg();
        boolean L = calc.level2();
        if(L == true){
            showPopup();
            calc.finish();
        }
    }
.....
}

CalculateAvg is the class I am instantiating. That class has a method called level2(), this is where I do some checks and return True or False as boolean. When I run the code, init() never gets called by onCreate(). I also tried writing the entire code of init inside onCreate itself, still same problem, onCreate never gets triggered.

CalculateAvg class

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;


public class CalculateAvg extends AppCompatActivity{
    public static final String SHARED_PREFS = "sharedPrefs";
    private static final String TAG = "Level1_0";
    level10 = sharedPreferences.getBoolean(LEVEL10, false);
    ........
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }
    public void init(){
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        ........
        // do my calculations here but init() never gets called by onCreate().
        // I even tried writing entire code inside onCreate but it also didn't work
    }
    public boolean level2(){
        boolean L = false;
        if(level10 == null){
            L = false;
        }
        else{
            L = true;
        }
        return L;
    }
}

Any idea why onCreate is not getting triggered when I instantiate it in my main class?

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
ssdesign
  • 2,743
  • 6
  • 37
  • 53
  • `onCreate` is invoked according [Activity Lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle). If you create instance like `new CalculateAvg()` it creates an instance of **CalculateAvg** but never call `onCreate`. For your requirements you simply can use class without **AppCompatActivity** – Md. Asaduzzaman Feb 02 '20 at 06:36
  • Thanks, but if I do not use AppCompatActivity, how can I read values from sharedpreferences? – ssdesign Feb 02 '20 at 06:41
  • Check my answer below – Md. Asaduzzaman Feb 02 '20 at 06:42

2 Answers2

1

According your requirements there is no need to use AppCompatActivity. You can simply use like class and pass context to access SharedPreferences.

public class CalculateAvg {

    private Context mContext;

    ....

    public CalculateAvg(Context context) {
        mContext = context;
        init();
    }

    public void init(){
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);

       ....
    }

    public boolean level2(){
        boolean L = false;
        if(level10 == null){
            L = false;
        }
        else{
            L = true;
        }
        return L;
    }
}

And instantiate CalculateAvg with your activity's context like below:

CalculateAvg calc = new CalculateAvg(Level1_0.this);
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

This Android not a java you can not call activity with creating new instance to call CalculateAvg from Level1_0 do below code in Level1_0 onCreate().

public class Level1_0 extends AppCompatActivity {
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent =new Intent(this,CalculateAvg.class);
    startActivity(intent);
}
}
haresh
  • 1,424
  • 2
  • 12
  • 18
  • Thanks Haresh. I can try this. One question though, I want to return some value by calling a method inside this activity. When I create new Intent, how can I call that method and get the returned value? – ssdesign Feb 02 '20 at 06:42
  • you can do that with startactivityforresult() where you can send data back to activity look for this : https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – haresh Feb 02 '20 at 06:50