Android progress dialog disappears - java

I am new to android programming. So, I am trying to download a file from internet in async task and showing progress dialog until download finished. But if i switch my applications in between, say I open another app, progress dialog disappears.
This is the code I am using to show progress dialog
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.setMessage("Backing Up...");
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
What am i doing wrong here?

First If you are using the asynctask it runs on UI Thread so you must be switching from the home button it puts your app on the pause you can override onPause and onResume methods

I solved it by adding these methods in my code.
#Override
public void onDestroyView()
{
if (progressDialog != null && getRetainInstance())
progressDialog.setDismissMessage(null);
super.onDestroyView();
}
#Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

Related

How to save the state of AsyncTask when the devise is turned?

I have an Android application that use an AsyncTask in one activity. It works. So if I try to turn the smartphone when the task is running, the progress dialog hidden and the AsyncTask died. Now I know to fixed this problem I must to implement this two method.
protected void onSaveInstanceState(Bundle outState) {}
protected void onRestoreInstanceState(Bundle savedInstanceState)
But I don't Know how to save the state of my AsyncTask.
This is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
apriConnessioneDB();
//recupero le impostazioni dell utenza
Impostazioni impostazioni = db.getImpostazioni();
if(impostazioni!=null){
dialog = ProgressDialog.show(
this,
"Login",
"Verifica dati in corso.");
syncLogin = new SyncLogin();
syncLogin.setDialog(dialog);
syncLogin.setCt(this);
syncLogin.setDb(db);
syncLogin.setUsername(impostazioni.getUsername());
syncLogin.setPassword(impostazioni.getPassword());
//syncLogin.setCt(this.getApplicationContext());
syncLogin.execute();
}
}
protected void onSaveInstanceState(Bundle outState) {
try{
super.onSaveInstanceState(outState);
final AsyncTask task = syncLogin;
}catch(Exception e){
Log.e("ERROR", e.getMessage());
}
}
By turn you mean screen orientation?
If so, I think that your problem might be that your Activity is being recreated with screen orientation. And with that, your AsyncTask.
Refer to this to handle runtime changes.
http://developer.android.com/guide/topics/resources/runtime-changes.html

Android make contol of ADmob hide addview window after click on it

I am developing an android application and I want close the window of view adds after I click on the adds window using Resume event.I put a rule in resume event but this rule make adds never show. so if there is a way to make the view adds disappears when I return to my application?
this is my code:
#Override
protected void onResume() {
// TODO Auto-generated method stub
if (adView != null) {
adView.destroy();
}
super.onResume();
}
this is the right code:
protected void onResume() {
// TODO Auto-generated method stub
adView.setAdListener(new AdListener() {
#Override
public void onAdOpened() {
// Save app state before going to the ad overlay.
}
#Override
public void onAdClosed() {
adView.setVisibility(View.GONE);
// TODO Auto-generated method stub
super.onAdClosed();
}
});
super.onResume();
}
use following to disappear the view.
adView.setVisibility(View.GONE):
set an onClickListener on adView then in onClick call this method.

Android exit app on close

Hi in my android app I included a splash screen after this activity whenever I pressed the back button it is going to the previous pages of MainActivity. But I needed to exit from application if user press back button from MainActivity. But now currently mainactivity is not the start activity and splash screen is the activity. So i checked some methods and I saw
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this will clear the previous activities.. But in MainActivty why I need an intent.. I gave
finish();
in every intent of each activity so the activity will be cleared. But that is affecting my app's entire structure .. whenever user press back button app is going to home page. So any one suggest a solution.. so that I can exit directly from MainActivity if user presses a back button.
this is my backpress code for mainActivity
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
and my splashscreen
WebView wv;
// Splash screen timer
private static int SPLASH_TIME_OUT = 4000;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_flash);
wv = (WebView) findViewById (R.id.webView1);
wv.setHorizontalScrollBarEnabled(false);
wv.loadUrl("file:///android_asset/Finally-320.gif");
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
}
for your query:
But I needed to exit from application if user press back button from MainActivity.
For that you need to write this in MainActivity
#Override
public void onBackPressed() {
finish();
}
Update:
put this line to exit from app:
moveTaskBack(true);
use System.exit(0) in onBackPressed() like this:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
System.exit(0);
}

android async splash screen not showing content view

in my app the main page contains, quite a few images to load on my upload manager activity so it can take a few seconds, depending on how many images there are. i planned on creating a splashscreen to do this loading while displaying an image which is not as bad as the default blank screen with title. i have done this, which should work and does, except the setcontentview() does run but does not display.
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
load l=new load();
l.execute(this);
}
class load extends AsyncTask<Activity, Object, Object>{
#Override
protected Object doInBackground(Activity... a) {
// TODO Auto-generated method stub
Log.i("ss", "splash");
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
return null;
}
}
}
does anybody have any suggestions?
and feel free to ask for details i don't think i have explained it all too well.
edit:
thank you guys for the quick responses.
however i believe the problem was that i wasn't using a splash screen for the correct purpose,
the processes involved in:
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
seem to finish instantly, meaning the images in my onCreate method weren't executing until after the splash screen. what i did instead is changed the loading of my grid into an asynktask, as apposed to just doing my images in there.
i now have it loading fast with the images appearing after a few seconds. i shall be implementing a progress dialog of some sort.
anyone else with a similar problem should prioritize making the loading more efficient as i have.
You are passing Context as this in l.execute(this) and in class load you've passed Activity instance.
You can do it in this way and it works like a charm for me
public class SplashScreen extends Activity{
private static int SLPASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SLPASH_TIME_OUT);
}
}
Override onPostExecute method in class load extends AsyncTask<Activity, Object, Object>{ class, which will run when your doInBackground method finishs image downloading.
In onPostExecute you can open your next activity
like
protected void onPostExecute(Void unused) {
Intent intent= new Intent(this, next.class);
startActivity(intent);
}

background music automatically stops

I have made an application with Splash Music. But whenever I go in preferences of app, the music automatically stops, and then never plays untill I restart the application. Same is the case when I open an activity, which tells whether phone is in "Normal" or in "Silent" Mode.
What is the reason for this weird behavior?
Here is the Splash music code where I check whether to play music or not..
public class SplashScreen extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
mp= MediaPlayer.create(SplashScreen.this, R.raw.got);
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
;
if(pref.getBoolean("music", true))
{
mp.start();
}
if(pref.getBoolean("loop", true))
{
mp.setLooping(true);
}
Thread timer= new Thread()
{
public void run()
{
try
{
sleep(5000);
Class ourclass = Class.forName("com.umer.practice2.Menu");
Intent myintent= new Intent(SplashScreen.this,ourclass);
startActivity(myintent);
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Thanks
It is because you are creating and playing your mediaplayer object on the main thread. When your activity is getting paused, you are finishing the activity which is stopping your mediaplayer. If you want to keep you music independent of the activity, offshore it to a service.
Running in a Service should help you.

Categories

Resources