I tring to create an animation to my activity.
The animation working as it need to but it working only once.
Intent i = new Intent(a, UserDataActivity.class);
i.putExtra("userData", t);
a.startActivity(i);
a.overridePendingTransition(R.anim.spin_anim, R.anim.static_anim);
I starting the activity from other thread if it's matters.
a is a pointer to the main activity.
I know that the overridePendingTransition is working when calling the startActivity and the finish methods because of that I don't need to kill the calling acticity.
I solved the problem by running the code in the UI Thread
a.runOnUiThread(new Runnable()
{
#Override
public void run()
{
Intent i = new Intent(a, UserDataActivity.class);
// putting in the intent the user data
i.putExtra("userData", t);
a.startActivity(i);
a.overridePendingTransition(R.anim.spin_anim, R.anim.static_anim);
}
});
I would like if someone will explain me why this didn't work on other thread?
Related
I have developed an app with start, pause, resume and finish buttons.
It works properly in the activity using thread and handler.
If the user clicks on the start button a thread is started and displays textviewHH:MM:SS time and the rest of the buttons work correctly as well.
Problem:
If the activity goes to background then how do I update the textview time? I have made services for this task but, how do I take the response from services to UI?
Please, could you give me any idea of how to do it or any other possible solution?
You can create CustomBroadcast
Here is sample code.
Try this, it will work..
In YourService.Java
public static final String BROADCAST_ACTION = "com.example.tracking.updateprogress";
intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);
In YourActivity.Java
registerReceiver(broadcastReceiver, new IntentFilter(YourService.BROADCAST_ACTION));
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Update Your UI here..
updateUI();
}
}
You can also pass data in Intent.
If you create a simple new Thread(new Runnable() {...}) within the Activity you can run UI manipulations with the runOnUiThread(new Runnable() { // your UI modify method }) Acitvity method. If the Activity go to the background the Thread is still running.
I have a trouble with the circular progressBar, I have it in background in a layout as invisible. When I want to start the new activity I show it setting visible the layout, it is shown good but the animation of the progressBar works bad it sometimes rotate a little then stop after rotate a little more... It isn't fluently. I try to solve with this but seems that didnt work:
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
progressBar.setIndeterminate(true);
progressLayout.setVisibility(View.VISIBLE);
final CustomOverlayItem i = item;
Runnable intentRunnable = new Runnable()
{
public void run()
{
Intent intent = new Intent(context,Next.class);
startActivityForResult(intent, RETURN_FROM_MONUMENTO);
}
};
intentRunnable.run();
}
});
Thanks for your answers.
The part where you receive the data back from the other activity would be interesting. Sending the intent in a Runnable shouldn't be necessary since that is a pretty inexpensive operation (I think). But receiving the result back can result in some heavy operations.
Stuttering animations are normally a sign that you are doing something on the UIThread you shouldn't be doing (or that the device simply is too slow to handle the load of all threads).
If you are doing the network related stuff in the next activity you launch. use AsyncTask in that so that will not block the UI thread and it will make the ProgressBar Animation Smooth.
Well, please don't ask me why, but I need to start a synchronous and blocking activity so that the program flow won't continue until it's finished. I know how to do a synchronous Dialog, but how can I do a synchronous activity?
Following are two approaches I tried but failed:
// In the 1st activity, start the 2nd activity in the usual way
startActivity(intent);
Looper.loop(); // but pause the program here
// Program continuse running afer Looper.loop() returns
....
// Then, in the second activity's onBackPressed method:
public void onBackPressed() {
// I was hoping the following quit() will terminate the loop() call in
// the first activity. But it caused an exception "Main thread not allowed
// to quit." Understandable.
new Hanlder().getLooper().quit();
}
I also tried to use another thread to achieve this:
// In the first activity, start a thread
SecondThread m2ndThread = new SecondThread(this);
Thread th = new Thread(m2ndThread, "Second Thread");
th.run();
synchronized(m2ndThread) {
m2ndThread.wait();
}
class SecondThread implements Runnable {
Activity m1stActivity;
SecondThread(Activity a) {
m1stActivity = a;
}
public void run() {
Looper.prepare();
Handler h = new Handler() {
public void handleMessage() {
Intent intent = new Intent(m1stActivity, SecondActivity.class);
m1stActivity.startActivity(intent); // !!!!!!!!!!!!!!
}
}
h.sendEmptyMessage(10); // let it run
Looper.quit();
}
}
However, this approach doesn't work because when I'm using the 1st activity to start the 2nd activity, the main thread is already in wait state and doing nothing. So the 2nd activity didn't even get created. (This is ironical: You have to use an activity to start an activity, but how can you do that when it's already in wait state?)
You can't.
That is all there is to it. You just can't do this. Activities execute on the main thread, always. Your main thread must be actively running its event loop. It is ALWAYS broken to block the main thread's event loop. Always.
All activities are asynchronous. But if you want to block the rest of your application flow until the activity finishes, you can inject a dependency on a flag that the activity sets. The most encapsulated way to do so would be to carry the flag along with the intent/activity/return flow. You could:
Declare the flag in that object's global scope:
boolean syncBlock = false;
int ASYNC_ACTIVITY_REQUEST_CODE = 1; // Arbitrary unique int
In the object's scope that starts the new activity include:
Intent asyncIntent = new Intent(this, AsyncActivity.class);
syncBlock = true;
startActivityForResult(asyncIntent, ASYNC_ACTIVITY_REQUEST_CODE);
while(syncBlock) {}
And in in the object that started the activity:
onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case ASYNC_ACTIVITY_REQUEST_CODE:
{
syncBlock = false;
break;
}
[...]
}
It's a crude hack, and if you're blocking in your UI thread (eg. in your MAIN activity) you're blocking everything, including other UI features your users will probably expect to respond but won't. It's a big nono, and you should learn to go with the async flow that makes apps work the Android way. But if you absolutely must...
I want to refresh the activity as i want thatwithout firing any event some work gets performed and activity calls by itself. So, i want to know is there any option in android to refresh the activity by itself.
You can do this by yourself through a Handler on which you call postDelayed(..)
http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)
Put this in your class:
private final Handler handler = new Handler();
make a function called: doTheAutoRefresh() that does:
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
doRefreshingStuff(); // this is where you put your refresh code
doTheAutoRefresh();
}
}, 1000);
}
Call this function in your onCreate.
NOTE: this is the basic approach. consider stopping this after onPause has been called and to resume it after onResume. Look at the handler class to see how to remove.
You can create a thread and and call the refresh() with the task you want to refresh
for other questions I've pulled the most effective ways to do this are:
finish();startActivity(getIntent());
OR
// Refresh main activity upon close of dialog box
Intent refresh =new Intent(this, ActivityWeAreIn.class);
startActivity(refresh);
Note: this also works with Activity objects or from Fragments using getActivity()
I am having a common problem doing animationdrawable in Android. I wanted to start an animation when the Activity starts, in the onCreate() method, but as many people have found, it doesn't work.
I have read lots of advice but nothing seems to work for me. If I start the animation in onClick() it works, it requires user input, not starting immediately.
I tried starting it in a separate thread in onCreate() but that doesn't work either. I read here:
http://code.google.com/p/android/issues/detail?id=1818
but none of the advice worked, or I couldn't understand it.
Can someone help?
I've faced similar problems, and switched to overriding onWindowFocusChanged() instead of onCreate() and onResume():
public void onWindowFocusChanged(boolean hasFocus)
{
if (hasFocus)
{
animation.start();
}
else
{
animation.stop();
}
}
I think you have to start the animation after initialization of the view in question is complete. You should be able to do something like this:
final ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);
tweenImage.setBackgroundResource(R.anim.cubicfacetween);
tweenImage.post(new Runnable() {
#Override
public void run() {
AnimationDrawable frameAnimation =
(AnimationDrawable) tweenImage.getBackground();
frameAnimation.start();
}
}
For setting src in imageView
((ImageView)findViewById(R.id.no_network_icon)).post(new Runnable() {
#Override
public void run() {
AnimationDrawable drawable = (AnimationDrawable) ((ImageView)findViewById(R.id.no_network_icon)).getDrawable();
drawable.start();
}
});
According to the link you provided you have to start the animation in a separate thread. In Java you do that by implementing the Runnable interface and start it with
Thread t = new Thread(new MyRunnable()); // MyRunnable inherits Runnable
t.start();
you can also write the code like this
new Thread(new Runnable() {
public void run(){
// some code that runs outside the ui thread.
}
}).start();
if you don't want to implement a whole new class. The latter is of course not that pretty but if you're making a small project it can be nice to know about.
Have you tried it this way or have you started your thread in another way?
Please also read Painless Threading that goes over what possibilities you have to perform actions outside the ui thread and how to post methods that runs on the ui thread from your own threads.
Edit: After reviewing the link you posted you have to wait a while before starting your thread, probably until onCreate is complete. According to the flowchart on this page you should be able to start your animation later, for instance in the onResume call. Have you tried starting it in a method called after onCreate?
You should only create things in onCreate, and "start" them in onStart or onResume.