24

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);
        ....
    }
    }
}
RomanKovalev
  • 852
  • 3
  • 10
  • 29
  • Why you need its instance. Its overridden method, it will have some activity context. But still if you want, try using `MyActivity.this` – Rajkiran Mar 15 '12 at 15:43
  • I use `this` but program crash when surfaceChanged called. Ok. I'll check again why program crash. I need this instance to call its method in context: `activity.getWindowManager().getDefaultDisplay() .getRotation()` – RomanKovalev Mar 15 '12 at 20:33

4 Answers4

35

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()
    
whlk
  • 15,487
  • 13
  • 66
  • 96
  • This is the way to go if you want store Activity instance on static variables & avoid memory leaks – Vincent Dec 01 '15 at 11:23
  • This is totally wrong, Android activities are recreated under memory pressure, in that case, you are holding a wrong reference of destroyed activity !! – Akash Kava Oct 10 '16 at 13:51
  • I meant that it could be used untill memory will pressured and could be updated every time when app returnd to foreground. – Евгений Шевченко Oct 13 '16 at 08:49
  • Miha_x64, singleton just an app structure model which could be applied to any object as an app structure part. So 1 - This code working fine which could be tested. 2 - Do not say about things you not understand. – Евгений Шевченко Mar 08 '17 at 23:19
  • I can confirm this solves memory leak issues, as LeakCanary reported a memory leak before this implementation, and it went away after. – barnacle.m Feb 27 '18 at 19:26
8
Activity a = (Activity) getContext();

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

elp
  • 8,021
  • 7
  • 61
  • 120
ivagarz
  • 2,434
  • 22
  • 22
4

I just set a variable in my main activity like so... public static Activity activity = this; then I can reference it from anywhere using: MainActivity.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.

This will work for any class that extends Activity, for example public class MainActivity extends Activity however you can call the variable from any class even if they don't extend Activity.

Hope this helps.

Luke Alderton
  • 3,198
  • 1
  • 23
  • 34
  • 11
    Memory leak, as stated below – MLProgrammer-CiM Dec 18 '12 at 10:44
  • 2
    This can definitely cause memory leaks by keeping reference to a static context (the activity). – dell116 Apr 10 '14 at 01:41
  • 3
    Don't you get the compile error `Cannot use this in a static context`? – hengxin May 13 '14 at 12:27
  • 1
    Please don't spread this kind of technique as it produces bad coding style, hard to maintain code base, and various problems such as memory leaks. Hope people would allot a time to read Android best practices and design patterns. – mr5 May 24 '17 at 07:45
0

Method this.getParent() works.