supertramp supertramp - 25 days ago 14
Android Question

Fragment not attached to Activity when returning from Async Task

I have added three fragment inside A_Activity. These are fragment_1, fragment_2, fragment_3

from fragment_1 I am calling an async class async_class

Here is my Fragment_1:

public class Fragment_1 extends Fragment {

ListView listView;
private TrainingGetData mTrainingGetData = null;
Context mContext;

public Fragment_1(){
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);

mTrainingGetData = new TrainingGetData(getContext());
mTrainingGetData.execute((String) null);
return view;
}


public void setTitle(Context context, ArrayList<String> ids, ArrayList<String> titles, final ArrayList<String> urls){

final Activity activity=(Activity) context;
final ArrayAdapter adapter=new ArrayAdapter(context, android.R.layout.simple_list_item_1, titles);
listView=(ListView) ((Activity) context).findViewById(R.id.listView_1);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(activity, AnotherActivity.class);
myIntent.putExtra("url", urls.get(position));
startActivity(myIntent);
});
}

}


In the async class I do some task in the background and in post execude I have added these code

public class TrainingGetData extends AsyncTask<String, String, String> {
Fragment_1 fragment_1=new Fragment_1();

public TrainingGetData(Context context){
mContext=context;
}

@Override
protected String doInBackground(String... params) {

try {
//do the processing
}
catch (JSONException e) {
e.printStackTrace();
return "JSON Data Parse Error";
}
catch (Exception e){
e.printStackTrace();
return "Unknown Error: Insert/Update Failed";
}
return null;
}

@Override
protected void onPostExecute(String message) {

if(success == 0 && message.equals("No files found")){
Toast.makeText(mContext, "Sorry No Data Found", Toast.LENGTH_SHORT).show();
}

if(success==1) {
//do the processing
fragment_1.setTitle(mContext, ids, titles, urls);
}
}
}


But when I return to the fragment_1 setTitle method and click on any list item it shows that the fragment_1 is not attached to the Activity.

What went wrong?
How can I solve this?

Thanks

Answer

But when I return to the fragment_1 setTitle method and click on any list item it shows that the fragment_1 is not attached to the Activity

Because currently creating new object of Fragment_1 for calling setTitle method.

Pass current Fragment Context in TrainingGetData class in same way as passing Context then call setTitle method. like:

Fragment_1 fragment_1;

public TrainingGetData(Context context,Fragment_1 fragment_1){
    mContext=context;
    this.fragment_1=fragment_1;
}

and create TrainingGetData object:

mTrainingGetData = new TrainingGetData(getContext(),Fragment_1.this);
Comments