Arunabha Mukhopadhyay Arunabha Mukhopadhyay - 1 month ago 6
Android Question

ProgressDialog not showing in TabActivity

I have a TabActivity with some tabs for user input. When the user is done with the input and presses the save button I proceed towards data saving. This is done by calling the method startDataTravershal() in each activity. I want to show a progressDialog when the button is pressed and dismiss it when the data save is done. however whatever I do the progressDialog is not showing.

my code :

public class HandHoldingTabUI extends TabActivity {

TabHost TabHostWindow;
TabSpec TabEntry;
TabSpec TabRectification;
TabSpec TabG26;
TabSpec TabU26;
TabSpec TabG27;
TabSpec TabBudget;
TabSpec TabBDC;
TabSpec TabTradeL;
TabSpec TabIncomeC;
TabSpec TabPCC;
TabSpec TabTAR;
TabSpec TabPNTA;
TabSpec TabPaperless;
TabSpec TabOthers;

Button btnSaveData = null;

ProgressDialog progressDialog = null;

public static HandHoldingTabUI context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handholding_tab_ui);
context=this;
UserDataHelper helper = new UserDataHelper(HandHoldingTabUI.this);
helper.deleteData("delete from "+UserDataHelper.tableMentorSurveyData);
helper.close();
init();
}

private void init() {
btnSaveData = (Button) findViewById(R.id.btnSaveData);
btnSaveData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = ProgressDialog.show(HandHoldingTabUI.this, "",
"Loading. Please wait...");
HandHoldingTabUI.this.runOnUiThread(new Runnable() {

@Override
public void run() {
saveData();
progressDialog.dismiss();
}
});

}
});
TabHostWindow = (TabHost)findViewById(android.R.id.tabhost);

createTab(TabEntry,"Entry","Entry", Entry.class); //Entry
createTab(TabRectification,"Rectification","Rectification", Rectification.class); // Rectification
createTab(TabG26,"Generation of form 26","Generation of form 26", G26.class); //Generation of form 26
createTab(TabU26,"Uploading of form 26","Uploading of form 26", U26.class); //Uploading of form 26
createTab(TabG27,"Generation of form 27","Generation of form 27", G27.class); //Generation of form 27
createTab(TabBudget,"Budget (Form 36)","Budget (Form 36)", Budget.class); //Budget (Form 36)
createTab(TabBDC,"Birth and Death Certificate","Birth and Death Certificate", BDC.class); //Birth and Death Certificate
createTab(TabTradeL,"Trade license","Trade license", TradeL.class); //Trade license
createTab(TabIncomeC,"Income Certificate","Income Certificate", IncomeC.class); //Income Certificate
createTab(TabPCC,"Provisional Caste Certificate","Provisional Caste Certificate", PCC.class); //Provisional Caste Certificate
createTab(TabTAR,"Tax Assessment Register","Tax Assessment Register", TAR.class); //Tax Assessment Register
createTab(TabPNTA,"Preparation of Non-tax Assessment","Preparation of Non-tax Assessment", PNTA.class); //Preparation of Non-tax Assessment
createTab(TabPaperless,"Paperless","Paperless", PaperLess.class); //Paperless
createTab(TabOthers,"Others","Others", Others.class); //Others

setUpCommonData();

}

private void setUpCommonData() {
SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
HandholdingCommonData.gpCode = getIntent().getStringExtra("gpCode");
HandholdingCommonData.userId = prefs.getString("userCode",null);
HandholdingCommonData.userGroup = prefs.getString("userGroup",null);
HandholdingCommonData.userChekInTime = prefs.getString("checkInTime",null);
HandholdingCommonData.userFileId = HandholdingCommonData.userId + "_"+HandholdingCommonData.userChekInTime;
}

private void createTab(TabSpec TabMenu,String TabName,String TabIndicator,Class TabActivity){

TabMenu = TabHostWindow.newTabSpec(TabName);
TabMenu.setIndicator(TabIndicator);
TabMenu.setContent(new Intent(this,TabActivity));
TabHostWindow.addTab(TabMenu);
}

private void saveData() {

UserDataHelper helper = new UserDataHelper(HandHoldingTabUI.this);
helper.deleteData("delete from "+UserDataHelper.tableMentorSurveyData);
helper.close();

for(int i = 0; i<getTabHost().getTabWidget().getTabCount(); i++) {
getTabHost().setCurrentTab(i);
Activity myActivity = this.getCurrentActivity();
if(myActivity instanceof Entry) {
Entry obj = (Entry) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof BDC) {
BDC obj = (BDC) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof Budget) {
Budget obj = (Budget) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof G26) {
G26 obj = (G26) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof G27) {
G27 obj = (G27) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof IncomeC) {
IncomeC obj = (IncomeC) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof Others) {
Others obj = (Others) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof PaperLess) {
PaperLess obj = (PaperLess) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof PCC) {
PCC obj = (PCC) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof PNTA) {
PNTA obj = (PNTA) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof Rectification) {
Rectification obj = (Rectification) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof TAR) {
TAR obj = (TAR) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof TradeL) {
TradeL obj = (TradeL) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
if(myActivity instanceof U26) {
U26 obj = (U26) myActivity;
obj.category = "Handholding Support";
obj.startDataTravershal();
}
}
proceedToShowDraft();
}

private void proceedToShowDraft() {
Intent intent = new Intent();
intent.setClass(HandHoldingTabUI.this,DraftUi.class);
intent.putExtra("gpCode",HandholdingCommonData.gpCode);
intent.putExtra("userGroup",HandholdingCommonData.userGroup);
intent.putExtra("userId",HandholdingCommonData.userId);
intent.putExtra("userFileId",HandholdingCommonData.userFileId);
startActivity(intent);
HandHoldingTabUI.this.finish();
}
}


I have tried these solutions but they were of no help :

Progress Dialog in Tab Layout in android

Show a progress dialog in android tab layout

android how to put progressdialog in TabActivity

i would greatly appreciate any help regarding this matter.

thanks,best regards.

EDIT:
according to the comment i have tried to do this :-

AsyncTask task = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
progressDialog = ProgressDialog.show(HandHoldingTabUI.this, "",
"Loading. Please wait...");
saveData();
return null;
}

@Override
protected void onPostExecute(Object o) {
progressDialog.dismiss();
proceedToShowDraft();
}
};


this is written on the onClick() of the button. but now when i click the button,nothing happens.

Answer

Please try changing your onClick() method like this:

@Override
public void onClick(View v) {
    progressDialog = ProgressDialog.show(HandHoldingTabUI.this, "",
            "Loading. Please wait...");
    new Thread(new Runnable() {

        @Override
        public void run() {
            saveData_1();
            HandHoldingTabUI.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    saveData_2();
                    progressDialog.dismiss();
                }
            });
        }
    }).start();
}

...

private void saveData_1() {
    UserDataHelper helper = new UserDataHelper(HandHoldingTabUI.this);
    helper.deleteData("delete from "+UserDataHelper.tableMentorSurveyData);
    helper.close();
}

private void saveData_2() {
    for(int i = 0; i<getTabHost().getTabWidget().getTabCount(); i++) {
        getTabHost().setCurrentTab(i);
        Activity myActivity = this.getCurrentActivity();
        if(myActivity instanceof Entry) {
            Entry obj = (Entry) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof BDC) {
            BDC obj = (BDC) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof Budget) {
            Budget obj = (Budget) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof G26) {
            G26 obj = (G26) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof G27) {
            G27 obj = (G27) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof IncomeC) {
            IncomeC obj = (IncomeC) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof Others) {
            Others obj = (Others) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof PaperLess) {
            PaperLess obj = (PaperLess) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof PCC) {
            PCC obj = (PCC) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof PNTA) {
            PNTA obj = (PNTA) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof Rectification) {
            Rectification obj = (Rectification) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof TAR) {
            TAR obj = (TAR) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof TradeL) {
            TradeL obj = (TradeL) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
        if(myActivity instanceof U26) {
            U26 obj = (U26) myActivity;
            obj.category = "Handholding Support";
            obj.startDataTravershal();
        }
    }
    proceedToShowDraft();
}

Please note that you should take into account what should happen when the activity is finished while the background thread is executing.

Comments