-1

I am developing an application in which I have a common activity, i.e. BaseActivity, I am extending it to all other activities in my application. I run few AsyncTask in BaseActivity to reflect in all other activities. My query is I need to pass values to this BaseActivity from one of my activities, say MainActivity

I tried to Class, Interface concepts, but not working for me. Suggest me, how to achieve this. If any of my reference code needed, comment it, I will post it.

EDITED

I need to pass this countvalue to BaseActivity. I cannot use Intent, because I am navigating to another activity say MainActivity2

MainActivity This is my AsyncTask code

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {

    @Override

    protected Integer doInBackground(String... params) {

        Integer result = 0;

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String response = streamToStringCount(httpResponse.getEntity().getContent());
                parseResultCount(response);
                result = 1;
            } else {
                result = 0;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }

        return result;
    }

    @Override

    protected void onPostExecute(Integer result) {

        //  isRefreshing = false;


        //layout.setRefreshing(false);

        super.onPostExecute(result);

    }
}


String streamToStringCount(InputStream stream) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
    String line;
    String result = "";
    while ((line = bufferedReader.readLine()) != null) {

        result += line;
    }
    if (null != stream) {

        stream.close();
    }
    return result;
}


private void parseResultCount(String result) {
    Detail_Item item;
    try {
        JSONObject posts = new JSONObject(result);


        //rstatus1=posts.optString("count");
        countValue=posts.optInt("count");
        countValue1=countValue;
        Log.i("countvalue", String.valueOf(countValue));

    }

    catch (JSONException e) {

        e.printStackTrace();

    }
}
john-salib
  • 724
  • 1
  • 12
  • 27
Anish Kumar
  • 478
  • 4
  • 12
  • 27

5 Answers5

1

store the values in SharedPreferences and retrieve them from any activity in your application.

Maher Nabil
  • 1,417
  • 1
  • 15
  • 19
1

In your AsyncTask define

public interface ResultListener {
    void onResultReady(Integer result);
}

create constructor which accepts ResultListener parameter

private ResultListener mRl = null;
public AsyncHttpTask (ResultListener rl) {
    mRl=rl;
}

at postExecute (it runs on main thread, you know) call listener

 protected void onPostExecute(Integer result) {
       super.onPostExecute(result);
       if (mRl!=null)
          mRl.onResultReady(result);
 }

Now if you have a reference to some activity which implements ResultListener which you need to pass a result you start the task this way:

AsyncHttpTask.ResultListener myActivity ;
...
(new AsyncHttpTask(myActivity)).execute(myParameters);

Your question now boils down to how your activities/fragments are started and coordinated so that you could get proper myActivity reference at proper place.

Serg
  • 22,285
  • 5
  • 21
  • 48
0

As per my understanding you want to pass some value from MainActivity(Which is extended BaseActivity) to BaseActivity.

Just create an method in BaseActivity like,

protected void setYourValue(Object obj){
    ..........
}

From your MainActivity just call this method and set whatever you want.

setYourValue("This is my value");

If you the MainAcitivity is not extended from BaseActivity,

 public static void setYourValue(Object obj){
    ..........
 }

In MainActivity,

BaseActivity.setYourValue("This is my value");

Please do some study on implementing sharedpreferrence.

Srinivasan
  • 4,481
  • 3
  • 28
  • 36
0

In your current Activity, create a new Intent:

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}

Refer Link

Community
  • 1
  • 1
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

you can achieve this by defining public static as all the instance of this or subclass will share it. This is really ugly solution which i don't recommend. It better to use Extras

Intent intent = new Intent(context, SecondActivity.class); intent.putExtra("Key", Value); startActivity(intent)

more info consult Doc https://developer.android.com/guide/components/intents-filters.html