简体   繁体   中英

how to move from an activity to another one from outside an activity android

I was wondering if there is a way to move the current activity by giving the order outside the activity you want to leave.

I show you what i got :

Here is my splashScreen activity :

public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    SQLHelper sqlHelper = new SQLHelper(this);
    InfosModel infos = sqlHelper.getInfos();
    RequestModel request = new RequestModel();
    if (infos.getToken().length() == 0){
        request.setType("password");
        new OAuthHelper().execute(request);
    }
 }
}

Here is my OAuthHelper :

public class OAuthHelper extends AsyncTask<RequestModel, Void, TokenModel> {

public OAuthHelper(){
}

@Override
protected void onPreExecute(){
}

@Override
protected void onPostExecute(TokenModel models){
    if (models == null){
        Log.i("infos", "You have no internet connection or the server is unreachable !");
    }
    else{
        Log.i("infos", "request's done with success !");
    }

}

@Override
protected TokenModel doInBackground(RequestModel... request) {
    try
    {
        try
        {
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://api.listopresto.com/oauth/token");
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
             if (request[0].getType() == "password"){
               /* Some Params */
             }
             else{
                 return (null);
             }
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
            HttpResponse response = httpclient.execute(httppost);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String s = reader.readLine();
            Log.i("infos", s);
            Gson gson = new Gson();
            TokenModel tokenObj;
            tokenObj = gson.fromJson(s, new TypeToken<TokenModel>(){}.getType());
            return (tokenObj);
        } catch (ClientProtocolException e) {
            Log.i("infos", "first");
            return (null);
        } catch (IOException e) {
            Log.i("infos", "second");
            return (null);
        }
    }
    catch (Exception e){
        Log.i("infos", "third");
        return (null);
    }
}   
}

If i paste my helper inside the spashscreen class, of course i'll be able to move to another activity, but this is a Helper, i mean i'll have to use it again from somewhere else and i of course don't want to pass by my splashScreen again ...

What i want to is moving from my splashScreen to the good one, depending on the answer i get from the server... i want to give that order in the postExecute method ..

Is there a way to do this ?

thanks you !! ;)

Pass the context to the constructor of Asynctask

new OAuthHelper(SplashScreen.this).execute(request);

Then

Context context;
public OAuthHelper(Context context){
    this.context= context;
}

Do background work in doInbackground . Return result in doInbackground .

Then in onPostExecute based on the response

context.startActivity(new Intent(context,SecondActivity.class));

The other option is to use a interface as a call back to the activity and then startActivity from the activity itself. Check the answer by blackbelt in the below link

How do I return a boolean from AsyncTask?

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