Reputation: 15
I set my ProfileActivity to extend to AppCompatActivity implementing View.OnClickListener. When the assigned button is clicked,it sends a StringRequest to a url using Volley getting a JSON format and parse it then store it in another class. The data stored is needed on the next activity that is started onClick which is myTabActivity. I need to make the ProfileActivity wait until the request, parsing, and storing is done in the ParseWorkOrderJSON.java before starting myTabActivity. Any thoughts? I am fairly new to Android too.
Here are snippets from my code:
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
public static final String JSON_URL = "/*I ommitted my URL on purpose here*/";
private TextView username_txt;
private Button fetch_data_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
username_txt = (TextView)findViewById(R.id.txt_username);
fetch_data_btn = (Button)findViewById(R.id.btn_fetch_data);
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String username = sharedPreferences.getString(Config.USERNAME_SHARED_PREF,"Not Available");
username_txt.setText(username);
sendRequest();
fetch_data_btn.setOnClickListener(this);
}
private void sendRequest(){
final ProgressDialog loading = ProgressDialog.show(this,"Fetching data.","Please wait...", false,false);
StringRequest stringRequest = new StringRequest(Request.Method.GET,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.substring(0));
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Something went wrong!");
Toast.makeText(ProfileActivity.this, "Something went wrong!", Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json){
ParseWorkOrderJSON pwoj = new ParseWorkOrderJSON();
pwoj.parseWorkOrderJSON(json);
}
@Override
public void onClick(View v){
sendRequest();
Intent intent = new Intent(ProfileActivity.this, MytabActivity.class);
startActivity(intent);
}
}
I am trying to use ProgressDialog while the request, parsing, and storing is happening then dismiss it when all is done. Only then the next activity is loaded which is myTabActivity. If you would like a snippet of ParseWorkOrderJSON please say so in the comments.
Upvotes: 0
Views: 222
Reputation: 7772
You don't need to slow down your app. You just need a better understanding of how asynchronous operation actually function.
Some operations (like HTTP request) may take a lot of time. If you do them on the main application thread (that is the thread that is in charge of updating and drawing the UI) your users may experience a frozen UI, irresponsive behaviour and all other bar stuff. In fact, Android has a build - in mechanism to prevent against this and, if an app takes too long to process a user input, it will try and force close it (the dreaded ANR dialog).
The way to deal with this is to offload those heavy operations aways from the main thread. Then you have another problem - you need to know when the operations finishes and react accordingly. This is called an async operation - it starts and returns a result in some moment in the future.
You are using Volley - a library, that does all this work for you - it creates a worker thread and uses it to execute HTTP requests, providing you with a callback to get notified when the request is complete.
The problem is that you are starting your activity when you are starting the Volley request. At this point in time there is still no data loaded, and nothing will get displayed. You should wait for the completion of the request, before continuing. How to know when the request is finished - it's when the onResponse()
method is called. To have everything working, you should move your code to start your second activity there. That way you will be sure that the data has been loaded and you can proceed to show it to the user.
Upvotes: 3