Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get activity instance

Tags:

Excuse me for simple question,I'm completely beginner java and android developer. How I can get the instance of Activity in setCameraDisplayOrientation when surfaceChanged is called?

public class MyActivity extends Activity {     private Camera mCamera;     private CameraPreview mPreview;     public final int cameraId = 0;     public Activity activity = null;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);      activity = this;           // Create an instance of Camera         mCamera = getCameraInstance();          // Create our Preview view and set it as the content of our activity.         mPreview = new CameraPreview(this, mCamera);         FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);         preview.addView(mPreview);     }      public void setCameraDisplayOrientation(Activity activity,                         int cameraId, android.hardware.Camera camera) {      }      public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     ...     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {         ...         setCameraDisplayOrientation(activity, cameraId, mCamera);         ....     }     } } 
like image 535
RomanKovalev Avatar asked Mar 15 '12 15:03

RomanKovalev


People also ask

How do I find my instance of activity?

You can also set it in the onCreate() method, just set up the variable at the top of your main activity class like this public static Activity activity; then in the onCreate() method just add activity = this; anywhere.

How do you create an instance of an activity?

Activity instances are always created by the Android system. This is because a lot of initializations have to be done for the activity to work. To create a new activity you call startActivity with an Intent describing the activity to start.

How do I get Mainactivity on Android?

1) Get your applicationContext by making your Android Application class a Singleton. 2) Get your ActivityManager class from the context. 3) Get a list of RunningTaskInfos using getRunningTasks() on the ActivityManager. 4) Get the first RunningTaskInfo element from the list which should be the most recent task launched.

What is SetContentView?

SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R. layout. somae_file). Here layoutfile is inflated to view and added to the Activity context(Window).


2 Answers

Here is a way to avoid memory leaks using static variable: make static weak reference to Activity instance that will be set in onCreate(Bundle) method.

  1. Write in your secondary class something like below:

    public Class SecondClass {     private static WeakReference<Activity> mActivityRef;     public static void updateActivity(Activity activity) {         mActivityRef = new WeakReference<Activity>(activity);     } 
  2. Then in onCreate(Bundle) method of your Activity class:

    @Override onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     SecondClass.updateActivity(this);     ... } 
  3. Use activity instance this way:

    mActivityRef.get() 
like image 121
Евгений Шевченко Avatar answered Oct 03 '22 14:10

Евгений Шевченко


Activity a = (Activity) getContext(); 

As long as you pass the current activity as a context in the constructor, as you are already doing.

like image 22
ivagarz Avatar answered Oct 03 '22 12:10

ivagarz