Hi! So i've been trying to get a solution for a very specific problem that
involves using Async tasks and dealing with the rotation and also a custom
header that contains a progressbar view(the small spinner one, including a
text view saying "Loading..."). I've followed
this<https://github.com/commonsguy/cw-android/blob/master/Rotation/RotationAsync/src/com/commonsware/android/rotation/async/RotationAsync.java>
example
to deal with the async tasks and the rotation, but i still have the problem
of the progressbar, if activity A launches a task and i imediately go to
activitity B, the progress bar in activity B should still appear visibile
even though the task was launched from activity A, how do i this without a
static reference for the progressbar view?, the postexecute method of the
async have to consider the possibility that the progressbar reference should
now be in another activity, right?
Here's my code for my parent activity where subactivities extend from:
public abstract class ParentClass extends Activity{
protected static ProgressBar progressHeader = null;
protected static TextView progressLoading = null;;
protected static int progressBarstate=ProgressBar.GONE;
protected static int themeId;
private ProgressBarThread task=null;
//this method will get the id for the layout
public abstract int getLayoutId();
//this method will launch respejcting oncreate logic for each activity
public abstract void initActivity();
//thi smethod will return window header menu context text string
public abstract String getWindowTitle();
//this method is used to refresh contents for a screen
public abstract void updateResultsInUi();
//nethod for definined specific theme
public abstract int getThemeId();
@Override
protected void onCreate(Bundle savedInstanceState) {
//set theme first for showing correct header
setTheme(getThemeId());
super.onCreate(savedInstanceState);
themeId=getThemeId();
//in case the screen will have a header
if(getThemeId()!=R.style.CustomThemeNoHeader){
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(getLayoutId());
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_window_title);
//set text header
((TextView)findViewById(R.id.header_title)).setText(getWindowTitle());
progressRefreshState(progressBarstate);
}else
{
setContentView(getLayoutId());
}
//execute subactivity logic
initActivity();
}
/**
* check for loading/spinner icon in
* case there's a thread working in background
*/
@Override
public void onRestart () {
super.onRestart();
if(getThemeId()!=R.style.CustomThemeNoHeader){
//Toast.makeText(this, "visible(0): " + ProgressBar.VISIBLE+" : Current :" +
getProgressBarstate(), Toast.LENGTH_LONG).show();
progressRefreshState(progressBarstate);
}
}
@Override
public Object onRetainNonConfigurationInstance() {
if(task!=null)
task.detach();
return(task);
}
/**
* refresh static references for both progress
* info views and set visibility state
*/
private void progressRefreshState(int state) {
progressHeader = (ProgressBar) findViewById(R.id.progresspinner);
progressLoading = (TextView) findViewById(R.id.progresstext);
progressHeader .setVisibility(state);
progressLoading.setVisibility(state);
}
//todo change location for getting session
protected String getSession(){
LogInManager login=new LogInManager(ParentClass.this);
try {
boolean session=login.getNewSession(
QuadrosMobileApplicationContext.getInstance().getUser(),
QuadrosMobileApplicationContext.getInstance().getPass());
if(session)
return getString(R.string.login_sucessful);
else
return getString(R.string.login_failed);
} catch (RequestException e) {
return e.getMessage();
}
}
/**
* do asynctask for background work
*/
public void doAsyncTask(){
task= (ProgressBarThread)getLastNonConfigurationInstance();
if(task==null){
task= new ProgressBarThread(this);
task.execute();
//add to the set of tasks
QuadrosMobileApplicationContext appliContext=
(QuadrosMobileApplicationContext)getApplicationContext();
appliContext.getAsyncTasks().add(task);
}else{
task.attach(this);
}
}
static public class ProgressBarThread extends AsyncTask<Void, Void, Void>{
ParentClass activity=null;
public ProgressBarThread(ParentClass activity) {
attach(activity);
}
@Override
protected void onPreExecute() {
Logger.write("ProgressBarThread ", " AsyncTask pre execution ",
Logger.INFO);
if(themeId!=R.style.CustomThemeNoHeader){
progressHeader.setVisibility(ProgressBar.VISIBLE);
progressLoading.setVisibility(ProgressBar.VISIBLE);
}
progressBarstate=ProgressBar.VISIBLE;
}
@Override
protected Void doInBackground(Void... params) {
Logger.write("ProgressBarThread ", "initialized", Logger.INFO);
activity.updateResultsInUi();
Logger.write("ProgressBarThread ", "Finish", Logger.INFO);
return null;
}
@Override
protected void onPostExecute(Void result) {
Logger.write("ProgressBarThread ", " AsyncTask post execution ",
Logger.INFO);
//remove this task from collection
QuadrosMobileApplicationContext appliContext=
QuadrosMobileApplicationContext.getInstance();
appliContext.getAsyncTasks().remove(this);
//check if there's more tasks in the collection
if(appliContext.getAsyncTasks().isEmpty()){
if(themeId!=R.style.CustomThemeNoHeader){
progressHeader.setVisibility(ProgressBar.GONE);
progressLoading.setVisibility(ProgressBar.GONE);
}
progressBarstate=ProgressBar.GONE;
}
}
void detach() {
activity=null;
}
void attach(ParentClass activity) {
this.activity=activity;
}
}
}
regards,
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en