1

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.

Community
  • 1
  • 1
  • to show a progressdialog you need to start in a background thread , like asynctask – Umar Ata Nov 01 '16 at 07:51
  • can you please provide any code/hints on how to do that? i looked into asynctask but could not figure out how to use that in this context – Arunabha Mukhopadhyay Nov 01 '16 at 08:19
  • why you need progressdialog in tabs creation – Umar Ata Nov 01 '16 at 08:29
  • its not when tabs are created but when i am starting the data travershal on button click. – Arunabha Mukhopadhyay Nov 01 '16 at 08:32
  • Use asynctask or volley to show progressdialog. – HaroldSer Nov 01 '16 at 08:33
  • write this after show of progressdialog Thread.sleep(5000); – Umar Ata Nov 01 '16 at 08:37
  • move this progressDialog = ProgressDialog.show(HandHoldingTabUI.this, "", "Loading. Please wait..."); in onpreexecute – Umar Ata Nov 01 '16 at 08:47
  • executing the asyncTask gives the error Can't create handler inside thread that has not called Looper.prepare(). i tried to execute it inside the run() method of a new Runnable() calling HandHoldingTabUI.this.runOnUiThread(new Runnable(){}.But it still throws the same error – Arunabha Mukhopadhyay Nov 01 '16 at 08:51
  • write Looper.prepare() in the first line of doinbackground – Umar Ata Nov 01 '16 at 08:52
  • following that i got the progressDialog but then encountered a new error that is "Only the original thread that created a view hierarchy can touch its views." i know it is because i am calling saveData() from within the doInBackground() of asynctask. but if i take out the method call from the aynctask then i won't get a continuous progressdialog. – Arunabha Mukhopadhyay Nov 01 '16 at 09:03

1 Answers1

0

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.

  • this throws the same exception "Only the original thread that created a view hierarchy can touch its views" – Arunabha Mukhopadhyay Nov 01 '16 at 09:26
  • Ah, sorry, just saw that saveData() does a lot of ui-related stuff. Maybe you could move all but UserDataHelper helper = new UserDataHelper(HandHoldingTabUI.this); and helper.deleteData("delete from "+UserDataHelper.tableMentorSurveyData); helper.close(); into the second Runnable of my post, so that it runs on the ui thread? –  Nov 01 '16 at 09:31
  • tried to move saveData() inside runOnUiThread.run() but no luck.The progressdialog only shows for just a second when the activity is finally ready to forward controle – Arunabha Mukhopadhyay Nov 01 '16 at 09:33
  • I really can't figure out what is the issue. my wild guess is that it has something to do with changing the current tab to a different one inside the loop of saveData(). but can't figure out the exact problem. – Arunabha Mukhopadhyay Nov 01 '16 at 09:37
  • What I was trying to say in my comment was: split your saveData() into two parts. The one part that can run in background is the three lines regarding UserDataHelper. The much bigger remaining part must be run on the ui thread, –  Nov 01 '16 at 09:41
  • thanks for the help.this did solve the problem by 90% (because the progressdialog is not spinning.it is frozen.but still its better than no progressDialog at all). Thanks for the help. – Arunabha Mukhopadhyay Nov 01 '16 at 09:47