-2

I follow this stackoverflow link Auto refresh the activity which refresh activity every 5 second i want to refresh activity once only on create not every 5 second waht do i do?

public void onCreate(Bundle savedInstanceState)  
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  this.mHandler = new Handler();

  this.mHandler.postDelayed(m_Runnable,5000);

}//onCreate

private final Runnable m_Runnable = new Runnable()
{
  public void run()
  {
    Toast.makeText(refresh.this,"in runnable",Toast.LENGTH_SHORT).show();

    refresh.this.mHandler.postDelayed(m_Runnable, 5000);            
  }

};//runnable
Cœur
  • 37,241
  • 25
  • 195
  • 267
Hayya Anam
  • 69
  • 9
  • 1
    I dont really know what you mean by refresh. Also I dont know if you want it to refresh 5 sec. after the create or with the calling of create. – xeed Apr 26 '13 at 11:00
  • i take picture from gallery using intent when intent finisg this activity not refresh and show pictures until i go back and come again on this activity – Hayya Anam Apr 26 '13 at 11:02
  • So your idea is to actually refresh every five seconds? Not only once? – xeed Apr 26 '13 at 11:04
  • no no not every 5 second just once on Oncreate – Hayya Anam Apr 26 '13 at 11:06
  • U can write the logic in onstart() or onresume() methods. – Noundla Sandeep Apr 26 '13 at 11:11
  • But your app gets refreshed OnCreate... Since it is reloaded. I srsly dont get. Check out this site for the activity lifecycle. http://developer.android.com/training/basics/activity-lifecycle/starting.html – xeed Apr 26 '13 at 11:11

1 Answers1

0

Since you described your actual problem in the comments, i'll answer this here.

You shouldn't refresh you app every 5 seconds. You should react to the intents result.

Solution No 1 (ActivityForResult approach)

This is how you do it:

private static final int SELECT_PHOTO = 100;

Start the Intent

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

This part is called on a result. And you should refresh your activity in this method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
        }
    }
}

EDIT: To make it clear: What happens is, that you create your activity. Call your intent. Your activity is getting paused onPause() and after the picking it gets resumed onResume(). So if you want to refresh your activity after it was paused. You write this behaviour in the onResume() Method. But still if you are starting an Intent for some result. You would use the above method to add the result to your activity.

Solution No 2 (general approach)

@Override
protected void onResume() {
    super.onResume();
    //Refresh here
}
xeed
  • 925
  • 8
  • 22