android App is launching automatically after minimizing app - java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"OnCreate()");
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.my_toolbar);
mTextview = findViewById(R.id.title);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment, new OneFragment());
//ft.commit();
ft.addToBackStack("frag");
ft.commitAllowingStateLoss();
}
}, 5000);
}
after minimizing my android app again its opening automatically.please help me to solve this problem

The Correct way to manage the handler is,
In your activity add this code,
private void initTasks() {
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
};
handler.postDelayed(runnable, 5000);
}
#Override
protected void onResume() {
super.onResume();
initTasks();
}
Then to stop opening your app after minimizing,
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacks(null);
}

Related

Is there a way to refresh webView every x seconds?

Now I have this function for reload webView:
public void reloadWebView() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
webView.reload();
}
}, 5000);}
And it is called on onCreate function
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reloadWebView();
}
The function works but it only runs once
Easy to solve as long as the Activity is running in foreground:
Handler handler = new Handler();
public void reloadWebView() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
webView.reload();
reloadWebView();
}
}, 5000);}
Note that handler is now a field, out of reloadWebView().
Now call it in your onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reloadWebView();
}
Here is code to update your webview after x seconds
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
webView.reload();
//here you can have your logic to reload webview
}
public void onFinish() {
// hide progress bar if any
}
}.start();

Why doubleclick on a custom button crashes an android app

I implemented a custom button and added a task to it with delay so it shows the animation.
When I double click it, it crashes. I want to make so it's only clickable once.
i have tried setEnabled(false);
i have tried setClickable(false);
i tried a variable that check if a button has been clicked and disables it.
public class Login extends AppCompatActivity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
}
}
As I wrote, I want that if the button has been clicked that it becomes unclickable.
Try this:
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
...
Explanation:
Your enable/disable block is outside of the onClick listener and it's in the onCreate method: in this way you call setEnable() method only when the activity was created.
But some time setEnable() can not work in case of very rapid click like explained in the second response here. In that case you can use a timer to check the elapsed time.
By the way, I think that your app crash because you don't handle in the right way the Handler. I suggest you also to add:
#Override
protected void onStop(){
handler.removeCallbacksAndMessages(null);
}
Try...
view.setOnClickListener(null);
...after your click event.

load 100% data using services while displaying splash screen

I have an Android App, which shows a "Splash Screen" for 3 seconds. After that, the MainActivity gets loaded.
Unfortunately the MainActivity takes additional ~4 seconds to load. On first startup even longer. However when the App is loaded, everything runs smooth.
I am using Async-Task for this, but desired result is not obtained and sometimes app crashes. Can someone help me please?
SplashScreen.java
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Fetchdata().execute();
}
private class Fetchdata extends AsyncTask {
#Override
protected Void doInBackground(Void... voids) {
Intent intent = new Intent(SplashScreen.this,FetchApiService.class);
startService(intent);
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return null;
}
#Override
protected void onPostExecute(Void result){
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}
#Override
public void onDestroy(){
super.onDestroy();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Some heavy processing
//starting services
Intent intent = new Intent(this,FetchApiService.class);
startService(intent);
}
}

ProgressDialog in runOnUiThread

I am trying to show progress dialog and update it inside runOnUiThread
but the progress bar never shown. when I replace the runOnUiThread with "new Thread" it work fine. But I want it to work with runOnUiThread
here is my code , I have deleted unnecessary codes
public class test extends Activity {
private ProgressDialog progress;
Handler progressBarHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testing();
}
public void testing() {
progress=new ProgressDialog(this);
progress.setMessage("Saving Progress");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setProgress(0);
progress.setMax(100);
progress.setCancelable(false);
progress.show();
runOnUiThread(new Runnable() {
#Override
public void run() {
//do some work
for (int i =0; i<100;i++){
//some work
progressBarHandler.post(new Runnable() {
#Override
public void run() {
progress.setProgress(finalCount);
}
});
}
}
}
Try this
public class MainActivity extends AppCompatActivity {
private ProgressDialog progress;
Handler workHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress=new ProgressDialog(this);
progress.setMessage("Saving Progress");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setProgress(0);
progress.setMax(100);
progress.setCancelable(false);
progress.show();
workHandler = new Handler(new HandlerThread("workHandlerThread").getLooper());
workHandler.post(new Runnable() {
#Override
public void run() {
//Do some work
runOnUiThread(new Runnable() {
#Override
public void run() {
progress.setProgress(/*work result*/);
}
});
}
});
}
}
The idea is you do the work on the HandlerThread bound to workHandler, and post the results back the the UI using runOnUiThread()

Timer() function not working when going to another activity

In my application i want to close it after 5 seconds using Timer() function.It works when i am in MainActivity but when i go to another activity then the application do not close.Now how to run this Timer() function in background if i switch activity.What to do in this case?
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
finish();
}
}, 5000); // Application will be closed after 5 seconds
You achieve this using broadcast receiver. in your activity which you want to finish you need to create broadcast receiver.
public class TestActivity extends Activity {
public static String intent_filter_finish = "com.test.finish";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerReceiver(finishReceiver,
new IntentFilter(intent_filter_finish));
}
#Override
protected void onDestroy() {
unregisterReceiver(finishReceiver);
super.onDestroy();
}
BroadcastReceiver finishReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
}
now in your second activity you need to send broadcast after 5 second e.g.
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
sendBroadcast(new Intent(TestActivity.intent_filter_finish));
}
}, 5000);
}
}
or other possible way is directly use postDelayed() method in your test activity e.g.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 5000);

Categories

Resources