在片段打开之前,progressDialog被关闭了

时间:2018-07-03 05:22:27

标签: java android fragment progressdialog

在打开新片段后尝试关闭ProgressDialog时遇到了一些问题。这是我的代码:

        ProgressFragment progressDialog = new ProgressFragment();
        progressDialog.setProgressMessage("Sending command, please wait.");
        progressDialog.show(fragment.getFragmentManager(), null);
        Handler handler = new Handler();
        handler.postDelayed(() -> {
            if (true) {
                ResultFragment resultFragment = new ResultFragment_();
                resultFragment.show(getFragmentManager(), null);
                progressDialog.dismiss();
            }
            else{
                // show error toast
            }
        }, 5000);

使用上面的代码,在打开新片段之前关闭了progressDialog。有什么方法可以将其设置为打开新片段,然后关闭进度对话框。

谢谢!

2 个答案:

答案 0 :(得分:1)

非常容易的想法是通过处理程序将消息发送到“活动”并关闭进度对话框。喜欢,

OnCreate片段

    <!DOCTYPE html>
<html>
<head>
<!-- BASE SETTINGS -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://fonts.googleapis.com/css?family=Aldrich' rel='stylesheet'>
<link href="styles/style.css" rel="stylesheet">

<title> Vamk Database </title>
<!-- BASE SETTINGS -->  

<!-- SCRIPT FOR AXIOS -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>   
<script
    src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous">
</script>
<!-- SCRIPT FOR AXIOS -->

<!-- SCRIPT FOR JQUERY UI -->
<script
    src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
    integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
    crossorigin="anonymous">
</script>
<!-- SCRIPT FOR  -->


<!-- SCRIPT FOR TABULATOR  -->
 <link rel="stylesheet" href="dist/css/tabulator.css"> 
<script type="text/javascript" src="dist/js/tabulator.js"></script>
<!-- SCRIPT FOR -->

<!-- SCRIPT FOR -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<!-- SCRIPT FOR --> 

<!-- DOWNLOAD LIBS -->
<script type="text/javascript" src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.2/jspdf.plugin.autotable.js"></script>

<script type="text/javascript">

//STORED PASSWORD AND USERNAME
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "extra.txt", false);
xhttp.send();

var x = xhttp.responseText;

var auther = x;
var validation = "";

// CHECKS IF THERE IS AUTH TOKEN IN DB
function getAuth(){
    var request = new XMLHttpRequest();
    request.open("GET", "http://127.0.0.1:8000/Valids/", false);
    request.setRequestHeader("Authorization", "Basic " + btoa(auther));
    request.setRequestHeader("X-CSRFToken", readCookie("csrftoken"));
    request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    request.send();
        array = JSON.parse(request.responseText);   
        if (array == "") {
            validation = false;
        }
        else {
            validation = true;
        }
    return validation;
}

//COOKIE CHECK
function readCookie(name) {
 var nameEQ = encodeURIComponent(name) + "=";
 var ca = document.cookie.split(';');
 for (var i = 0; i < ca.length; i++) {
 var c = ca[i];
 while (c.charAt(0) === ' ') c = c.substring(1, c.length);
 if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
 }
 return null;
}
</script>

</head>

<body>

<script type="text/javascript">
    getAuth();
</script>

    <div id="root">  

<div id="header" class="test">
        <h1> VAMK Laboratory Database </h1>
</div>

<div id="options">  

    <button id="show-ops" type="button" value="Show options" onclick=""> Toggle Options </button>
    <button onclick="window.location.href='authindex.html'" id="return">Return</button> 
    <button onclick="logout()" id="login">Logout</button>

    <div id="ops" style="display:none">
    <button id="download-pdf" class="downloads"> Download PDF </button>
    <button id="download-csv" class="downloads"> Download CSV </button>
    <button id="download-json" class="downloads"> Download JSON </button>
    <button id="download-xlsx" class="downloads"> Download XLSX</button>

    <button id="add-row" class="rows"> Add Row </button>
    <button id="del-row" class="rows"> Delete Rows </button>

    </div>

</div>

<div id="noacc" style="display=none">

<h1> No Access </h1>

</div>

<div id="example-table"></div>  

<script>

if ( validation != true) {
    console.log(validation);
    document.getElementById("noacc").style.display='block';
    document.getElementById("options").style.display='none';
    document.getElementById("example-table").style.display='none';      
}

else {  

    document.getElementById("noacc").style.display='none';

// DIV HIDING SCRIPT
$("#show-ops").click(function(){
        if (document.getElementById("ops").style.display == 'none') 
        document.getElementById("ops").style.display='block';
        else
        document.getElementById("ops").style.display='none';
});


/*
// Performing a GET request ! 
    axios({
    method: 'GET',
    url: 'http://127.0.0.1:8000/Vamk_Rs/',
    responseType: 'json'
}) 


  .then(function(response){  
  */

    var response = new XMLHttpRequest();
    response.open("GET", "http://127.0.0.1:8000/Vamk_Rs/", false);
    response.setRequestHeader("Authorization", "Basic " + btoa(auther));
    response.setRequestHeader("X-CSRFToken", readCookie("csrftoken"));
    response.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    response.send();

var array = JSON.parse(response.responseText);

console.log(array);

    if (response.status ==  200) {

// POSTING UPDATED CELL TO REST

$("#example-table").tabulator({
    cellEdited:function(cell){
            var data = cell.getData();
            var url = data.url;
            var posting = JSON.stringify(data);
            var update = new XMLHttpRequest();
                update.open("PUT", url, true);
                update.setRequestHeader("Authorization", "Basic " + btoa(auther));
                update.setRequestHeader("X-CSRFToken", readCookie("csrftoken"));
                update.setRequestHeader("Content-Type", 'application/json');
                    update.send(posting); 
},

// TABLE LAYOUT 
    layoutColumnsOnNewData:true,
    layout:"fitColumns",
    responsiveLayout:"hide",
    placeholder:"No Data Set",
    pagination:"local",
    paginationSize:50,
    addRowPos:"top",
    selectable: true,
    align: 'center',
    columns:[
        {title:"Select Row", field:"", width:'100'},
        {title:"Category", field:"category", headerFilter:"input", align:"center", editor: "input"},
        {title:"Value", field:"strenght", headerFilter:"input", editor:"input", width: '80'},
        {title:"Unit", field:"unit", headerFilter:"input", editor:"input", width: '80'},
        {title:"Part Number", field:"manufacturer_part_number", headerFilter:"input", editor:"input", width: '160'},
        {title:"Location", field:"storing_location", headerFilter:"input", editor:"input"},
        {title:"EC19 Location", field:"storing_location_ec19", headerFilter:"input", editor:"input"},
        {title:"Type", field:"ptypename", headerFilter:"input", editor:"input"},
        {title:"Description", field:"description", headerFilter:"input", editor:"input", width: '180'}, 
        {title:"Order Number Elfa", field:"order_number_elfa", headerFilter:"input", editor:"input", width: '150'},
        {title:"Order Number Farnell", field:"order_number_farnell", headerFilter:"input", editor:"input", width: '160'},
        {title:"Amount", field:"a", editor:"input", width: '80', headerFilter:"input"},
        {title:"Tilattu", field:"tilattu", editor:"input", width: '80', headerFilter:"input"},
        {title:"Kuittaus", field:"kuittaus", editor:"input", width: '80', headerFilter:"input"}, 
    ],

});

// DOWNLOAD OPTIONS!!!

//trigger download of data.csv file
$("#download-csv").click(function(){
    $("#example-table").tabulator("download", "csv", "data.csv");
});

//trigger download of data.json file
$("#download-json").click(function(){
    $("#example-table").tabulator("download", "json", "data.json");
});

//trigger download of data.xlsx file
$("#download-xlsx").click(function(){
    $("#example-table").tabulator("download", "xlsx", "data.xlsx", {sheetName:"My Data"});
});

//trigger download of data.pdf file
$("#download-pdf").click(function(){
    $("#example-table").tabulator("download", "pdf", "data.pdf", {
        orientation:"portrait", //set page orientation to portrait
        title:"Example Report", //add title to report
    });
});

// ADD AND DELETE ROWS

//Add row on "Add Row" button click
$("#add-row").click(function(){
    $("#example-table").tabulator("addRow", {});
    var data = {
    "category": "",
    "strenght": "",
    "manufacturer_part_number": "",
    "storing_location": "",
    "storing_location_ec19": "",
    "ptypename": "",
    "tolerance": "",
    "description": "",
    "order_number_elfa": "",
    "order_number_farnell": null,
    "a": "",
    "tilattu": "",
    "kuittaus": ""
}
    posting = JSON.stringify(data);

    var request = new XMLHttpRequest();
            var url = "http://127.0.0.1:8000/Vamk_Rs/";
                request.open("POST", url, true);    
                request.setRequestHeader("Authorization", "Basic " + btoa(auther));
                request.setRequestHeader("X-CSRFToken", readCookie("csrftoken"));
                request.setRequestHeader("Content-Type", 'application/json');
                    request.send(posting); 
});


//Delete row on "Delete Row" button click
$("#del-row").click(function(){
  var rows = $('#example-table').tabulator("getSelectedRows");
    console.log(rows);
  $.each(rows, function(index, row) {
    console.log(rows);
    row.delete();
//SENDING INFO OF DELETED ROWS TO REST  
    var data = row.getData();
    console.log(data);
    posting = JSON.stringify(data);

    var request = new XMLHttpRequest();
            var url = data.url;
                request.open("DELETE", url, true);  
                request.setRequestHeader("Authorization", "Basic " + btoa(auther));
                request.setRequestHeader("X-CSRFToken", readCookie("csrftoken"));
                request.setRequestHeader("Content-Type", 'application/json');
                    request.send(posting);  
  });
});

// INIATE THE TABLE
 $("#example-table").tabulator("setData", array); 
}
}

function logout(){
    var req = new XMLHttpRequest();
            req.open("GET", "http://127.0.0.1:8000/Valids/", false);
            req.setRequestHeader("Authorization", "Basic " + btoa(auther));
            req.setRequestHeader("X-CSRFToken", readCookie("csrftoken"));
            req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            req.send();

            var tmpStr = req.responseText;
            var newStr = tmpStr.substring(1, tmpStr .length-1);
            console.log("newStr= " + newStr);
            var obj = JSON.parse(newStr);
            console.log("OBJ = " + obj);
            var url = obj.url;

            var del = new XMLHttpRequest();
            del.open("DELETE", url, false);
            del.setRequestHeader("Authorization", "Basic " + btoa(auther));
            del.setRequestHeader("X-CSRFToken", readCookie("csrftoken"));
            del.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            del.send();         
            window.location.href='index.html';
} 


</script> 



</div>

</body>

</html>

活动中

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

   Message msg = new Message();
   msg.what = 1223;
   Activity1.mHandler.sendMessage(msg);

}

答案 1 :(得分:0)

在片段A中声明

private ProgressFragment progressDialog;

public void CloseProgressFragment()
{
    progressDialog.dismiss();
}

请勿关闭postDelayed中的对话框

在Fragment B中,借助FragmentManager获取您的Fragment A,并调用方法close

string fragATag = typeof(FragmentA).Name

FragmentA fragmentA = FragmentManager.FindFragmentByTag(fragATag);

fragmentA.CloseProgressFragment();