简体   繁体   中英

How to get the context in the main class constructor?

I searched everywhere and I decided to write here. I'm trying to achieve a connection when my app starts. My problem is that I need to pass the context to the AsyncTask (or create ProgressDialog earlier in the constructor) - but I also need the context.

The most important thing: I can't move things from constructor to onCreate, cause it will make a new Client every time I open the app, go back etc.

The problem is here that when the constructor is fired, I don't have context yet.

Any ideas?

Already tried with this: StartConnectionTask(this); but this is the same thing.

Found this: http://briandolhansky.com/blog/2013/7/11/snippets-android-async-progress implemented it and still the same. Context is null.

public class MainActivity extends AppCompatActivity {
public Client client;
public Button loginButton;
public static String host = "192.168.1.2";
public static int tcp_port = 8888, udp_port = 8889;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_layout);
}

public MainActivity(){
    client = new Client();
    client.start();
    Network.register(client);
    StartConnectionTask s = new StartConnectionTask(MainActivity.this);
    s.execute();
}

public class StartConnectionTask extends AsyncTask<Void, Void, Boolean> {

    private ProgressDialog conDialog;

    public StartConnectionTask(MainActivity activity) {
        conDialog = new ProgressDialog(activity);
    }
    @Override
    protected void onPreExecute() {
        conDialog.setTitle("Connecting with a server...");
        conDialog.setMessage("Connecting with " + host);
        conDialog.show();
        conDialog.setCancelable(false);
    }

you need to shift your code from MainActivity() to at the end of onCreate .

In android conventionally onCreate is the method where your activity starts as per activity lifecycle. you can easily pass context from here by passing this

As asked by OP, here's how you can override your Application class

public class App extends Application {

    private static App mInstance;
    private Client mClient;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        // magic and stuff
        // put anything here and in anywhere in your app, activity
        // fragment, service, whatever you want
        // do App.getInstance().whatever()
        // like App.getInstance().getClient()

        mClient = new Client();
    }

    public static synchronized App getInstance() {
        return mInstance;
    }

    public static Client getClient() {
        return mClient;
    }

}

And specify this in AndroidManifest.xml

    <application
        android:name=".App"
        ... >

Activity extend context class . when a activity is start it flow her life cycle so activities constructor no need you put this constructor code into on create method.

Rahul Tiwari's answer will do. But in case, if you want to implement Application class to get the context throughout your aplication, try this.

public class App extends Application {

public static Context context;

@Override
public void onCreate() {
    // Do your stuff here
    context = getApplicationContext();
    super.onCreate();
}  ...

And add this to your manifest

 <application android:label="@string/app_name"
    android:name="com.your.package.App ">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM