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); .... } } }
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.
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.
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.
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).
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.
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); }
Then in onCreate(Bundle) method of your Activity class:
@Override onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SecondClass.updateActivity(this); ... }
Use activity instance this way:
mActivityRef.get()
Activity a = (Activity) getContext();
As long as you pass the current activity as a context in the constructor, as you are already doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With