I have tried a lot of ways to trigger the backPressed() event when the progressDialog is displayed. But none works. If I provide progDialog.setcancelable(true); I am able to dismiss the progressDialog but still the onBackPressed() doesn't get triggered.
When ProgressDialog is active if yiou press back key to perform yopur own operations you have to set setOnCancelListener to the progressdialog.
write your logic inside onCancel() method example the whole logic that you have written in onBackPressed() event those things you have to write here.
Sample code
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CancelProgressDialog extends Activity {
ProgressDialog myProgressDialog = null;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Create a very simple button */
Button b = new Button(this);
this.setContentView(b);
b.setText("Show ProgressBar...");
b.setOnClickListener(myProgressBarShower);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
System.out.println("...any key is pressed....");
if (keyCode == KeyEvent.KEYCODE_BACK)
{
System.out.println("...BackButton is pressed...");
if( (myProgressDialog!= null) && myProgressDialog.isShowing()){
myProgressDialog.dismiss();
}
}
return super.onKeyDown(keyCode, event);
}
/** OnClickListener that fakes some work to be done. */
OnClickListener myProgressBarShower = new OnClickListener() {
// #Override
public void onClick(View arg0) {
// Display an indeterminate Progress-Dialog
myProgressDialog = ProgressDialog.show(CancelProgressDialog.this,
"Please wait...", "Doing Extreme Calculations...", true);
myProgressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
System.out.println("...cancel button is pressed");
// perform your task here
}
});
myProgressDialog.setCancelable(true);
}
};
}
Thanks
Deepak
Related
I am trying to login with username and password. If username is correct then goes to another Activity otherwise stay in current Activity.
If I am login the first time it works successfully but second time gives some error.
error.....
04-21 07:41:13.915: E/AndroidRuntime(2125): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
04-21 07:41:13.915: E/AndroidRuntime(2125): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
04-21 07:41:13.915: E/AndroidRuntime(2125): at android.os.AsyncTask.execute(AsyncTask.java:534)
04-21 07:41:13.915: E/AndroidRuntime(2125): at com.bis.storyanimationapp.LoginActivity$1.onClick(LoginActivity.java:35)
code:
LoginActivity
//enter code here
package com.bis.storyanimationapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import com.bis.storyanimationapp.asynchtask.AsynchTask1;
import com.bis.storyanimationapp.view.LoginView;
import com.example.storyanimationapp.R;
public class LoginActivity extends Activity {
LoginView loginView;
AsynchTask1 objLoginAsycTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
loginView=new LoginView(this);
super.onCreate(savedInstanceState);
setContentView(loginView.getLayoutmain());
objLoginAsycTask=new AsynchTask1(LoginActivity.this,loginView);
loginView.getBtnLogin().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
objLoginAsycTask.execute("");
//objLoginAsycTask.cancel(true);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
AsynchTask1
package com.bis.storyanimationapp.asynchtask;
import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;
import com.bis.storyanimationapp.LoginActivity;
import com.bis.storyanimationapp.MainActivity;
import com.bis.storyanimationapp.view.LoginView;
public class AsynchTask1 extends AsyncTask<String,integer, String> {
Activity activity;
Context context;
private ProgressDialog signUpProcess;
LoginView view;
// private AsyncInterface asyInter;
public AsynchTask1(Activity activity,LoginView view){
this.activity=activity;
this.view=view;
context=activity.getApplicationContext();
}
#Override
protected void onPreExecute() {
super.onPreExecute();
signUpProcess = ProgressDialog.show(this.activity, "Please Wait !!!",
"Submitting Data For Registration!!!", true, false);
Toast.makeText(context, "inpree ex.",10).show();
}
#Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
signUpProcess.dismiss();
Toast.makeText(context,"post",5).show();
myMethod(result);
}
private void myMethod(String result) {
// TODO Auto-generated method stub
if(view.getEdUserName().getText().toString().equals("abc")){
Intent i=new Intent(context,MainActivity.class);
activity.startActivity(i);
//this.activity.finish();
}else{
Toast.makeText(context,"invalid user name",5).show();
// Intent i=new Intent(context,LoginActivity.class);
// activity.startActivity(i);
//
}
}
#Override
protected String doInBackground(String... params) {
return view.getEdUserName().getText().toString();
//return null;
}
}
As #Raghunandan suggest , create a new instance of the asyntask every time:
public void onClick(View v) {
new AsynchTask1().execute("");
}
});
You try this codes :
loginView.getBtnLogin().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
objLoginAsycTask=new AsynchTask1(LoginActivity.this,loginView);
objLoginAsycTask.execute((Void) null);
}
});
}
AsyncTask instances can only be used one time.
Instead, just call your task like new AsynchTask1().execute("");
From the AsyncTask API docs
Try to create a new instance of AsyncTask every time you log in.
#Override
public void onClick(View v) {
AsyncTask1 at = new AsyncTask1()
at.execute()
}
I have a ListView with a button on it. When I click the Button I have a AlertDialog with a EditText on it that pops up. When the users enters data into the EditText on the AlertDialog it goes out and updates a SQLite Database. When the original ListView shows back up it is blank. When I exit the app and return the app the data entered in the AlertDialog shows up. I need the new data to show up after the AlertDialog closes.
package com.wmason.testcreator;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.os.Bundle;
//import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.app.ListActivity;
import android.widget.Button;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
public class MainActivity extends ListActivity {
private DbManagement mdbManager;
private TestProcessor tp;
SimpleCursorAdapter notes;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lookup);
mdbManager = new DbManagement(this);
tp = new TestProcessor(this);
mdbManager.open();
fillData();
Button testingCsv =(Button)findViewById(R.id.btnTestCsv);
testingCsv.setOnClickListener(ChokeSlam);
fillData();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
fillData();
}
private OnClickListener ChokeSlam = new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AssetManager aM = getAssets();
try{
//This line of code opens the AlertDialog
tp.ProcessInboundStream(aM,"Book1.csv",mdbManager);
fillData();
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
};
protected void onListItemClick(ListView l, View v, int position, long id) {
/*
boolean b;
b=mdbManager.deleteTests(id);
*/
//fillData();
Intent i = new Intent(this,DisplayTests.class);
i.putExtra("ID",Long.toString(id));
startActivity(i);
}
private void fillData(){
Cursor testCursor = mdbManager.fetchAllTests();
startManagingCursor(testCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{DbManagement.Gen_Test_Name};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1};
//R.layout.
// Now create a simple cursor adapter and set it to display
notes =
new SimpleCursorAdapter(this, R.layout.testrows, testCursor, from, to);
setListAdapter(notes);
notes.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
They way I figured this out was to use the following method
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
fillData();
}
Basically as soon the AlertDialog closes it fires off this method and it goes out and re-populates the ListView
Try this....
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
I do something like this in the Activity file where the dialog is required.
AlertDialog dialog = dialogViewer.returnEditDialog();
dialog.show();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
refreshView();
}
});
The OnDismissListener is called every time an ALertDialog fragment is closed.
when "don't keep activities" is selected on Setting's Developer Options progress dialog is not dismissing.
Actually, I am displaying the Progerss bar,for initialize the Application, When I am going to Activity A to Activity B and then came to Activity A, the Progress bar is not dismissing after initialize completed.
Below is my Code,
First Activity
package com.example.donotkeepactivitiesalive;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public static final int MESSAGE_PROGRESS_BAR_INITIALIZING = 1;
private Dialog m_cObjDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(getClass().getName() + "before InitializeEnvironment >>>>>>");
new InitializeEnvironment().execute();
System.out.println(getClass().getName() + "after InitializeEnvironment >>>>>>");
Button lb = (Button) findViewById(R.id.button1);
lb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent lIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(lIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
protected Dialog onCreateDialog(int id) {
m_cObjDialog = new ProgressDialog(this);
m_cObjDialog.setTitle("Initializing");
((ProgressDialog) m_cObjDialog).setMessage("Initializing the Application");
m_cObjDialog.setCancelable(false);
return m_cObjDialog;
}
class InitializeEnvironment extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
System.out.println(getClass().getName() + "onPreExecute >>>>>>");
super.onPreExecute();
showDialog(MESSAGE_PROGRESS_BAR_INITIALIZING);
}
#Override
protected String doInBackground(String... aurl) {
System.out.println(getClass().getName() + " doInBackground >>>>>>");
return null;
}
#Override
protected void onPostExecute(String unused) {
dismissDialog(MESSAGE_PROGRESS_BAR_INITIALIZING);
System.out.println(getClass().getName() + " onPostExecute >>>>>>");
}
}
}
Second Activity
package com.example.donotkeepactivitiesalive;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Is there any way to track this problem and work even if "don't keep activities" is enabled
I solved this, by checking the Bundle object is null or not.If the Bundle object is null then call the AsynTask class, else don't call,
if(null == savedInstanceState) {
new InitializeEnvironment().execute();
}
But make sure that your previous Activity is not overriding onActivityResult()
I have made a simple android project with a single activity containing three buttons and a seek bar,Now i want is that when i press 1st button the seek bar should directly progressed 33%(without showing progress),when 2nd button pressed the seek bar should progressed again by 33% second time and when third button pressed the seek bar should full progressed..my code is as below:
Main.java
package com.example.seekabardemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends Activity {
Button btn,btn1,btn2;
SeekBar sk;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn1=(Button)findViewById(R.id.button2);
btn2=(Button)findViewById(R.id.button3);
sk=(SeekBar)findViewById(R.id.seekBar1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Thank you....Please help me..!
Please help me ..thank you..!
write this three statements in click events of each button respectively.
sk.setProgress(sk.getMax()/3);
sk.setProgress(sk.getMax()*2/3);
sk.setProgress(sk.getMax());
To set a seekBar progress just :
sk.setProgress(ProgressToSet);
to get the maximum possible progress just:
sk.getMax();
to get a porcentage of a seekbar just:
sk.setProgress(sk.getMax()/100*YourPercentage);
I am displaying a progress bar using Async task class and simulatneously in parallel operation , i want to retrieve a string array from a function of another class that takes some time to return the string array.
The problem is that when i place the function call in doing backgroung function of AsyncTask class , it gives an error in Doing Background and gives the message as cant change the UI in doing Background ..
Therefore , i placed the function call in post Execute method of Asynctask class . It doesnot give an error but after the progress bar has reached 100% , then the screen goes black and takes some time to start the new activity.
How can i display the progress bar and make the function call simultaneously.??plz help , m in distress
here is the code
package com.integrated.mpr;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Progess extends Activity implements OnClickListener{
static String[] display = new String[Choose.n];
Button bprogress;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
bprogress = (Button) findViewById(R.id.bProgress);
bprogress.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bProgress:
String x ="abc";
new loadSomeStuff().execute(x);
break;
}
}
public class loadSomeStuff extends AsyncTask<String , Integer , String>{
ProgressDialog dialog;
protected void onPreExecute(){
dialog = new ProgressDialog(Progess.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
}
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
for(int i = 0 ;i<40;i++){
publishProgress(5);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
dialog.dismiss();
String y ="abc";
return y;
}
protected void onProgressUpdate(Integer...progress){
dialog.incrementProgressBy(progress[0]);
}
protected void onPostExecute(String result){
display = new Logic().finaldata();
Intent openList = new Intent("com.integrated.mpr.SENSITIVELIST");
startActivity(openList);
}
}
}
You can't dismiss the dialog in doInBackground() - even dismissing a dialog needs the UI task. Move dialog.dismiss() to onPostExecute() of the AsyncTask.