Hello,
I have some kind of a special question...
I am new in Android programming and I wonder if it's posible to restart an application after the home-button stopped it. Since I know, the application is not destroyed, only stopped.
So, I thought doing something like this:
Code:
protected override void OnStop()
{
base.OnStop();
StartActivity(new Intent(this, typeof(MyActivity)));
}
Obviously that does not work. How can I "refocus" my Activity after the home-button closed it?
Thanks a lot!
Kuehner
Hello,
I have news. Restart works when I do this:
protected override void OnStop()
{
base.OnStop();
if (!guiHandler.TestStopped)
{
Intent intent = new Intent(this, this.Class);
StartActivity(intent);
}
//Finish();
}
My problem now:
I have to finish my old activity to start a new one. But I only want to restart a stopped activity without destroying it. Is this possible?
start the intent and finish the intent at the same time.
How do I do that?
Intent intent = new Intent(first.class, second.class);
startActivity(intent);
finish();
replace first.class and second.class with the classname.
Ok, thanks for your help. Now I understand.
The problem is that I do not want my activity to finish. When the user presses the home-button, the activity runs in background (for a couple of minutes) and there I want my activity to refocus again (without restarting, it's more "refocusing", so that the old state appears again).
I do not want my activity to finish!
i already tried it too.. but found nothing.. i solved my problem so.. :/
Related
So I am trying to use in intent to launch an app drawer (gridView of icons on device) from an image. I have an onClickListener for the image and the intent inside the listener. Like this:
ImageView sphere = (ImageView)findViewById(R.id.sphere);
OnClickListener sphere_listener = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent( HomeTest3.this, Grid.class);
startActivity(i);
}
};
sphere.setOnClickListener(sphere_listener);
}
What am I doing wrong? The main activity (HomeTest3) shows up just fine, then when I click the image to launch the second activity (Grid) the app force-closes. I tried to see what was going wrong using the ddms, but all I could see was "Null Java Pointer Exception" or something like that. How do I fix this problem?
How do you update an activity that implements on click listener WITHOUT needing to click something? (e.g. with a timer)
What is it you are trying to do? Why the new thread?
I need to update button, background images and text based on a timer instead of only updating when something is clicked
I don't understand what you mean by "only updating when something is clicked."
the images used (e.g. back ground, or buttons) will not change (update) unless a button is clicked. So its basically waiting for something to be pressed (any button) before it updates any imgaes
So let me get this straight; you're changing the images associated with layout controls in a timer of some kind? What kind of timer implementation? Is the timer in its own thread?
yeah. I tried making another class for the time but it doesn't work for updating the view of the main activity that I want. So I am trying to use:
Timer timer2 = new Timer();
timer2.scheduleAtFixedRate(Title(), 1, 1000);
Title() is defined as: public TimerTask Title()
NEVERMIND!! I GOT IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
// stuff to do
}
});
}};
t.schedule(scanTask, 1, 1000);
I am making an app that plays sounds in a loop based on a timer. after I exit the program using finish(); the sounds keep playing in background anyway. Any ideas of why this is and how I can fix it (besides having to use a task killer every time)
thanks
hyperbyteX said:
I am making an app that plays sounds in a loop based on a timer. after I exit the program using finish(); the sounds keep playing in background anyway. Any ideas of why this is and how I can fix it (besides having to use a task killer every time)
thanks
Click to expand...
Click to collapse
finish() just closes the activity, it does *NOT* close the app. Threads will continue to run, services will continue to run, broadcast receivers will continue to receive, etc...
You need to actually stop/cancel the timer if you want it to stop.
I recommend reading this (and then re-reading it again): http://developer.android.com/guide/topics/fundamentals.html and this http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
hyperbyteX said:
I am making an app that plays sounds in a loop based on a timer. after I exit the program using finish(); the sounds keep playing in background anyway. Any ideas of why this is and how I can fix it (besides having to use a task killer every time)
thanks
Click to expand...
Click to collapse
The timer probably uses the system timer service, Which calls your sound file again and again.
Stop the timer in the onDestory function of your activity.
how exactly do I stop it?
hyperbyteX said:
how exactly do I stop it?
Click to expand...
Click to collapse
Take a look at the documentation from google. probably something like .stop(timer)
don't know
heres the code Im using:
// set timer
TimerTask scanTask = null;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
// stuff to run goes here
}
}
});
}};
t.schedule(scanTask, 1, 50); // time how often stuff happens
I've tried many ways to kill it but nothing seems to work
hyperbyteX said:
heres the code Im using:
// set timer
TimerTask scanTask = null;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
// stuff to run goes here
}
}
});
}};
t.schedule(scanTask, 1, 50); // time how often stuff happens
I've tried many ways to kill it but nothing seems to work
Click to expand...
Click to collapse
Did you try TimerTask .cancel(); ?
Dark3n said:
Did you try TimerTask .cancel(); ?
Click to expand...
Click to collapse
yeah I tried that but it gives me an error saying "Cannot make a static reference to the non-static method cancel() from the type TimerTask"
Ok, thank you very much. I had to tweak it a bit but I got it!
Hello! I'm new to this forum. I'm developing an Android app, I have some problems with the functionality of a timer and I don't figure out why. I would need some ideas.
In my application I have 2 activities: one is with the levels of a game, where you can chose between them and the second one is with the game itself. In the second activity I have a CountDownTimer which tells me when the game must finish. I have a progressBar assigned to that timer.
CountDownTimer mCountDownTimer; -global variable
In onCreate I have:
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer=new CountDownTimer(90000,1000) {
int i=0;
@override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i); }
@override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
}
};
mCountDownTimer.start();
I have also overriden the native back button from Android to go to the first activity, the one with the levels and there I try to stop the counter, but it doesn't seem to work at all. The counter doesn't stop, and the other functions don't work as well.
Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
mCountDownTimer.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
return;
How could I solve this? Thank you so much!
Here is your problem:
CountDownTimer mCountDownTimer; -global variable
There are no global variables in Java, only instance variables. Since you didn't post your full code I'm going to make a few assumptions here...
public class myAwesomeTimerClass {
CountDownTimer mCountDownTimer;
// bla bla
}
Then in your second activity/class, you should refer to the class:
instead of:
mCountDownTimer.cancel();
try:
myAwesomeTimerClass.mCountDownTimer.cancel();
Good luck.
Hello,
I got a question. I'm trying to develop an app with some traffic light, it is made out of open source images. It is from a learn to develop android apps couse from udemy. I wanted to add something, I thought about a button with an automatic change function and stop function, with an interval to change the lights on and off. Do you guys have any idea how to do that?
Thanks in advance,
dev_freak
dev_freak said:
Hello,
I got a question. I'm trying to develop an app with some traffic light, it is made out of open source images. It is from a learn to develop android apps couse from udemy. I wanted to add something, I thought about a button with an automatic change function and stop function, with an interval to change the lights on and off. Do you guys have any idea how to do that?
Thanks in advance,
dev_freak
Click to expand...
Click to collapse
I think you can use this :
Code:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Put below your new function. */
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 5000);
Second parameter is the interval in seconds. (eg: 5 seconds = 5000)
eng.ahmed.android said:
I think you can use this :
Code:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Put below your new function. */
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 5000);
Second parameter is the interval in seconds. (eg: 5 seconds = 5000)
Click to expand...
Click to collapse
A bit late, but it didn't work