run onresume() method when i change tab "ontabchange()" in a view - android

I put the code in onResume() method for it to run each time when i load it again by tab click but problem is now that data load first time from server in to list view when I click first time on tab and when I change the tab and load it again it force close and gives "array index out of bound exception". I think it is because it not removes previous loaded data and so how to remove or reload new data on tab click so that exception not occur? This means before loading new data via onResume() how to delete old data?
protected void onPause() {
super.onPause();
}
protected void onResume()
{
super.onResume();
**new ProgressTask6().execute();**
}
private class ProgressTask6 extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private Context context;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(OpeningToday.this);
dialog.setMessage("Processing...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing())
{
dialog.dismiss();
setListAdapter(new MyAdapter(OpeningToday.this));
}
}
#Override
protected Boolean doInBackground(String... args) {
try{
} catch (Exception e){
Log.e("tag", "error", e);
return false;
}
return null;
}
class MyAdapter extends BaseAdapter implements OnClickListener
{
}
#Override
public int getCount() {
} }
/* Not implemented but not really needed */
#Override
public Object getItem(int position) {
return null;
}
/* Not implemented but not really needed */
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View ConvertView, ViewGroup parent)
{
View v = inflater.inflate(R.layout.listitem_layout, parent, false);
// Log.i("array galoijewdh..",keywordresulttab.array_galleryname[position]);
Log.i("saurabh trivedi","saurabh trivedui");
// Variables.a=3;
String gallerynames = keywordresulttab.array_galleryname[position];
String addresses = keywordresulttab.array_address[position];
TextView tv = (TextView) v.findViewById(R.id.barrio);
tv.setText(gallerynames);
tv = (TextView) v.findViewById(R.id.ciudad);
tv.setText(addresses);
((BaseAdapter)(getListAdapter())).notifyDataSetChanged();
return v;
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
}

Initialize the index / delete data in onPause() which is the opposite of onResume().
As a rule of thumb (according to activity lifecycle) - clean what you need in the opposite method -
onCreate() - onDestroy()
onStart() / onRestart() - onStop()
onResume() - onPause()

Related

Prevent multiple touches after touching an item on the list

I have a main activity with a list view, an onItemClickListener() event is associated to the list view and when I touch an element I start an AsyncTask that does some work. I would like to disable the other touches that the user could make to the other elements of the list during the async task execution and re-enable them when the async task ends.
How could I do?
This is my activity
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private ListView newList;
private ArrayAdapter<String> newArrayAdapter;
private MyAsyncTask asyncTask;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressbar);
newArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
newList = findViewById(R.id.pairedDevicesListView);
newList.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
newList.setOnItemClickListener(null); //need something like this
asyncTask = new MyAsyncTask(this);
asyncTask.execute();
}
#Override
public void onBackPressed() {
asyncTask.cancel(true);
super.onBackPressed();
}
private class MyAsyncTask extends AsyncTask<Void,Void,Void>{
Context context;
public MyAsyncTask(Context context){
this.context = context.getApplicationContext();
}
#Override
protected Void doInBackground(Void... voids) {
// do some stuff
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressBar.setVisibility(View.GONE);
newList.setOnItemClickListener();
//does not work or I don't know what to pass
}
}
}
Try this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (asyncTask == null || asyncTask.getStatus() == AsyncTask.Status.FINISHED) {
asyncTask = new MyAsyncTask(this);
asyncTask.execute();
}
}
Be careful with onBackPressed, or you may call a function on a null object.
#Override
public void onBackPressed() {
if (asyncTask != null) {
asyncTask.cancel(true);
}
super.onBackPressed();
}
You could write something like this in your onItemClickListener:
newList.setEnabled(false);
And then in your onPostExecute method write:
newList.setEnabled(true);

Android viewpager AsyncTask finish when page changed

I have view pager. My viewpager contains 3 fragments. In the first fragment I have an AsyncTask class. I parsed JSON with AsyncTask and I can show it in listview. (everything is ok)
I have one problem AsyncTask which I have in the first fragment does not finish when I go to the next fragments. When I am in the second fragment my AsyncTask is also running. How can I write code to cancel my AsyncTask when viewpager's page changed?
This is my source (this is the first fragment source; another fragment source is the same but the only difference is the Server Url):
public class StradaChefs1 extends Fragment {
public static CustomerStatistic stat;
private ConnectionDetector con;
private AlertDialogManager alert = new AlertDialogManager();
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.strada_chefs_1, container,
false);
stat = new CustomerStatistic();
con = new ConnectionDetector(getActivity());
if (!con.isConnectingToInternet()) {
alert.showAlertDialog(getActivity(),
"You have not internet connection");
} else {
stat.execute("my urlllllllll"); // geo
}
return rootView;
}
public class CustomerStatistic extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = ProgressDialog.show(getActivity(), "Please Wait... ",
"Loading... ");
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected String doInBackground(String... params) {
return Utils.getJSONString(params[0]);
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONArray mainJson = new JSONArray(result);
String first = mainJson.getString(0);
JSONObject jobject = new JSONObject(first);
String image = jobject.getString("image");
String String_title = jobject.getString("title");
String String_name = jobject.getString("name");
String String_desc = jobject.getString("description");
String second = mainJson.getString(1);
} catch (JSONException e) {
e.printStackTrace();
}
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
#Override
public void onResume() {
Log.e("DEBUG", "onResume of HomeFragment");
super.onResume();
}
#Override
public void onStop() {
super.onStop();
if (stat != null && stat.equals(AsyncTask.Status.RUNNING)) {
stat.cancel(true);
Toast.makeText(getActivity(), "finished", Toast.LENGTH_SHORT)
.show();
}
}
}
This is a viewpager java code
public class TabbedActivity1 extends Fragment {
private StradaChefs1 mfragment1;
private StradaChefs2 mfragment2;
private StradaChefs3 mfragment3;
private StradaChefs4 mfragment4;
SectionsPagerAdapter mSe;
public static final String TAG = TabbedActivity1.class.getSimpleName();
ViewPager mViewPager;
private ArrayList<Fragment> fragmentList;
public static TabbedActivity1 newInstance() {
return new TabbedActivity1();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_item_one_1, container, false);
mSe=new SectionsPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) v.findViewById(R.id.pager1);
CirclePageIndicator circle=(CirclePageIndicator)v.findViewById(R.id.circleindicator1);
mViewPager.setAdapter(mSe);
circle.setViewPager(mViewPager);
mfragment1 = new StradaChefs1();
mfragment2 = new StradaChefs2();
mfragment3 = new StradaChefs3();
mfragment4 = new StradaChefs4();
fragmentList = new ArrayList<Fragment>();
fragmentList.add(mfragment1);
fragmentList.add(mfragment2);
fragmentList.add(mfragment3);
fragmentList.add(mfragment4);
mViewPager.setPageTransformer(false, new PageTransformer() {
#Override
public void transformPage(View page, float position) {
page.setRotationY(position * -40);
}
});
return v;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
How can I solve this problem?
The FragmentPagerAdapter keeps additional fragments, besides the one shown, in resumed state, so you can't use onPause and onResume for starting/stopping the AsyncTask. The solution is to implement a custom OnPageChangeListener and create a new method for when the fragment is shown.
1) Create LifecycleManager Interface The interface will have two methods and each ViewPager’s Fragment will implement it. These methods Are as follows:
public interface FragmentLifecycle {
public void onPauseFragment();
public void onResumeFragment();
}
2) Let each Fragment implement the interface
3) Implement interface methods in each fragment - in onPauseFragment stop the AsyncTask, in onResumeFragment start it
4) Call interface methods on ViewPager page change You can set OnPageChangeListener on ViewPager and get callback each time when ViewPager shows another page
5) Implement OnPageChangeListener to call your custom Lifecycle methods
Listener knows the new position and can call the interface method on new Fragment with the help of PagerAdapter. I can here call onResumeFragment() for new fragment and onPauseFragment() on the current one.
I need to store also the current fragment’s position (initially the current position is equal to 0), since I don’t know whether the user scrolled from left to right or from right to left. See what I mean in code:
private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
int currentPosition = 0;
#Override
public void onPageSelected(int newPosition) {
FragmentLifecycle fragmentToShow = (FragmentLifecycle)pageAdapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
FragmentLifecycle fragmentToHide = (FragmentLifecycle)pageAdapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
currentPosition = newPosition;
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
public void onPageScrollStateChanged(int arg0) { }
};
I didn't write the code. Full tutorial here
When you start the asynctask set the flag isRunning=true
when you are trying to jump from one fragment to other it mean
as per fragment lifecycle your are pausing and stoping your current fragment
so in onStop method of fragment you can check isRunning flag of asynctask is true if yes the
cancel the asyntask
its my logic hope it will help you to achieve your requirement

Handling async call inside onItemClick()

I have an implementation of onItemClick() while selecting from a list of items. However inside
onItemClick() I need to perform a check via a library call that is async. Basically within onItemClick() a call to XYZ.checkConnection(booleanCallback); is made which is async.
And then in booleanCallback(int status) I can check for the status. What kind of pattern can I use to handle this scenario?
public class Tester extends Activity {
private ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
new CheckConnection(list.getAdapter().getItem(position))
.execute();
}
});
}
private class CheckConnection extends AsyncTask<Void, Void, Void> {
private Object o;
public CheckConnection(Object o) {
this.o = o;
}
#Override
protected void onPreExecute() {
// Do things like initiate progress bar etc
}
#Override
protected Void doInBackground(Void... params) {
// Do connection check
return null;
}
#Override
protected void onPostExecute(Void result) {
int status = 0;
// determine status
booleanCallBack(status, o);
}
}
private void booleanCallBack(int status, Object o) {
// perform UI related
}
}
you use the delegate approach if you want a modular class
I have essentially given you a starting point,this can be tweaked based on what you want.I didnt exactly get your context.hence the generalised answer

Finish an activity from Fragment

In my application I have 2 fragment, Fragment A and Fragment B.On both these fragments Iam doing some XML parsing and populating listviews.I want to implement a splash screen for my app.So that it display during parsing and population of listviews and finishes when its done. For this, I have created an activity SplashActivity.I have Implemented asyntask on FragmentA and called the SplashActivity on preexecute section.Now, when the app launches SplashActivity get started.But I cant finish this acitivity on postexecute of FragmentA. getActivity().finishActivity() is not working.Please some one help me to solve this issue or suggest me another method to implement Splash screen on Fragment Activity. Thanks in advance....
here is my FragmentA=>
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating layout
View v = inflater
.inflate(R.layout.headlines_fragment, container, false);
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i("Tag", "onCreateView");
// We set clear listener
loading = (ProgressBar) getActivity().findViewById(R.id.progressBar1);
if (Headlines.headflag == "malayalam") {
urls = "http://www.abc.com/rssfeeds/19_18_17_25/1/rss.xml";
}
if (Headlines.headflag == "english") {
urls = "http://www.abc.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
}
new ProgressAsyncTask().execute();
MainActivity.refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new ProgressAsyncTask().execute();
}
});
}
public void populate_listview() {
newsList = new ArrayList<HashMap<String, String>>();
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
newsList.add(map);
MarqueeStr = MarqueeStr + " *** " + Title[i];
}
}
public void StartProgress() {
new ProgressAsyncTask().execute();
}
public class ProgressAsyncTask extends AsyncTask<Void, Integer, Void> {
int myProgress;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
myProgress = 0;
Intent intent = new Intent(getActivity(), Splash.class);
startActivityForResult(intent, 5); //Here I called Splash Activity
loading.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
getActivity().finishActivity(5);// Here I tried to finish flash activity
if (Title == null) {
final AlertDialog.Builder alertbox = new AlertDialog.Builder(
getActivity());
alertbox.setMessage("Error in connection.Do you want to retry");
alertbox.setPositiveButton("retry",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// getActivity().finish();
// Intent intent = new Intent(getActivity(),
// MainActivity.class);
// startActivityForResult(intent,1);
}
});
alertbox.setNegativeButton("exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// getActivity().finish();
}
});
alertbox.show();
}
list = (GridView) getActivity().findViewById(R.id.grid);
// Getting adapter by passing xml data ArrayList
adapter = new HeadlinesAdapter(getActivity(), newsList);
list.setAdapter(adapter);
loading.setVisibility(View.INVISIBLE);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
"fonts/karthika.TTF");
MainActivity.flashnews.setText(MarqueeStr);
if (Headlines.headflag == "malayalam") {
MainActivity.flashnews.setTypeface(tf);
}
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myintent = new Intent(
"com.abc.malayalam2headlinespodcast.PODCAST");
Bundle mybundle = new Bundle();
mybundle.putInt("number", position);
myintent.putExtras(mybundle);
startActivity(myintent);
}
});
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
parse();
if (Title != null) {
populate_listview();
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
}
}
public static void parse() {
//parsing done here
}
}
You should not implement your Splash as an Activity but rather as a DialogFragment. Simply style that dialog fragment so that it appears fullscreen.
Concerning showing:
SplashDialogFragment splashDlg = new SplashDialogFragment();
splashDlg.show(getSupportFragmentManager(), "SplashScreen");
Then closing:
SplashDialogFragment splashDlg = (SplashDialogFragment) getSupportFragmentManager().findByTag("SplashScreen");
splashDlg.dismiss();
just to answer the question which is your title actually "To finish an activity from Fragment", it's always a good way to use interface to interact from Fragment to Activity as per the docs: Communicating with Activity from Fragment. So one must always declare an interface in Fragment which is to be implemented in your activity something like below:
public class SplashActivity extends Activity implements FragmentA.FragmentListenerCallback {
public void onFragmentInteraction() {
//finish your activity or do whatever you like
finish();
}
}
public class FragmentA extends Fragment {
private FragmentListenerCallback fragmentListenerCallback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
fragmentListenerCallback = (FragmentListenerCallback) activity;
}
public interface FragmentListenerCallback {
void onFragmentInteraction();
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(fragmentListenerCallback != null){
fragmentListenerCallback.onFragmentInteraction();
}
}
}

Android save data from nested AsyncTask onPostExecute after screen rotation

I have spent many hours looking for a solution to this and need help.
I have a nested AsyncTask in my Android app Activity and I would like to allow the user to rotate his phone during it's processing without starting a new AsyncTask. I tried to use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance().
I am able to retain the task; however after rotation it does not save the result from onPostExecute() to the outer class variable. Of course, I tried getters and setters. When I dump the variable in onPostExecute, that it is OK. But when I try to access to the variable from onClick listener then it is null.
Maybe the code will make the problem clear for you.
public class MainActivity extends BaseActivity {
private String possibleResults = null;
private Object task = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.task = getLastNonConfigurationInstance();
setContentView(R.layout.menu);
if ((savedInstanceState != null)
&& (savedInstanceState.containsKey("possibleResults"))) {
this.possibleResults = savedInstanceState
.getString("possibleResults");
}
if (this.possibleResults == null) {
if (this.task != null) {
if (this.task instanceof PossibleResultWebService) {
((PossibleResultWebService) this.task).attach();
}
} else {
this.task = new PossibleResultWebService();
((PossibleResultWebService) this.task).execute(this.matchToken);
}
}
Button button;
button = (Button) findViewById(R.id.menu_resultButton);
button.setOnClickListener(resultListener);
}
#Override
protected void onResume() {
super.onResume();
}
OnClickListener resultListener = new OnClickListener() {
#Override
public void onClick(View v) {
Spinner s = (Spinner) findViewById(R.id.menu_heatSpinner);
int heatNo = s.getSelectedItemPosition() + 1;
Intent myIntent = new Intent(MainActivity.this,
ResultActivity.class);
myIntent.putExtra("matchToken", MainActivity.this.matchToken);
myIntent.putExtra("heatNo", String.valueOf(heatNo));
myIntent.putExtra("possibleResults",
MainActivity.this.possibleResults);
MainActivity.this.startActivityForResult(myIntent, ADD_RESULT);
}
};
private class PossibleResultWebService extends AsyncTask<String, Integer, Integer> {
private ProgressDialog pd;
private InputStream is;
private boolean finished = false;
private String possibleResults = null;
public boolean isFinished() {
return finished;
}
public String getPossibleResults() {
return possibleResults;
}
#Override
protected Integer doInBackground(String... params) {
// quite long code
}
public void attach() {
if (this.finished == false) {
pd = ProgressDialog.show(MainActivity.this, "Please wait...",
"Loading data...", true, false);
}
}
public void detach() {
pd.dismiss();
}
#Override
protected void onPreExecute() {
pd = ProgressDialog.show(MainActivity.this, "Please wait...",
"Loading data...", true, false);
}
#Override
protected void onPostExecute(Integer result) {
possibleResults = convertStreamToString(is);
MainActivity.this.possibleResults = possibleResults;
pd.dismiss();
this.finished = true;
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (this.possibleResults != null) {
outState.putString("possibleResults", this.possibleResults);
}
}
#Override
public Object onRetainNonConfigurationInstance() {
if (this.task instanceof PossibleResultWebService) {
((PossibleResultWebService) this.task).detach();
}
return (this.task);
}
}
It is because you are creating the OnClickListener each time you instantiate the Activity (so each time you are getting a fresh, new, OuterClass.this reference), however you are saving the AsyncTask between Activity instantiations and keeping a reference to the first instantiated Activity in it by referencing OuterClass.this.
For an example of how to do this right, please see https://github.com/commonsguy/cw-android/tree/master/Rotation/RotationAsync/
You will see he has an attach() and detach() method in his RotationAwareTask to solve this problem.
To confirm that the OuterClass.this reference inside the AsyncTask will always point to the first instantiated Activity if you keep it between screen orientation changes (using onRetainNonConfigurationInstance) then you can use a static counter that gets incremented each time by the default constructor and keep an instance level variable that gets set to the count on each creation, then print that.

Categories

Resources