Java 获取活动实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9723106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:44:09  来源:igfitidea点击:

Get activity instance

javaandroidandroid-activity

提问by psct

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?

请原谅我的简单问题,我完全是java和android开发者的初学者。调用surfaceChanged时如何获取setCameraDisplayOrientation中的Activity实例?

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);
        ....
    }
    }
}

采纳答案by Евгений Шевченко

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.

这是使用静态变量避免内存泄漏的一种方法:对将在 onCreate(Bundle) 方法中设置的 Activity 实例进行静态弱引用。

  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()
    
  1. 在你的中学课上写下类似下面的内容:

    public Class SecondClass {
        private static WeakReference<Activity> mActivityRef;
        public static void updateActivity(Activity activity) {
            mActivityRef = new WeakReference<Activity>(activity);
        }
    
  2. 然后在 Activity 类的 onCreate(Bundle) 方法中:

    @Override
    onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SecondClass.updateActivity(this);
        ...
    }
    
  3. 以这种方式使用活动实例:

    mActivityRef.get()
    

回答by Luke Alderton

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.

我只是在我的主要活动中设置一个变量,就像这样......public static Activity activity = this;然后我可以从任何地方使用: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.

您也可以在 onCreate() 方法中设置它,只需像这样public static Activity activity;在主活动类的顶部设置变量,然后在 onCreate() 方法中添加activity = this;任何地方即可。

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

例如,这适用于扩展 Activity 的任何类,public class MainActivity extends Activity但是您可以从任何类调用变量,即使它们不扩展 Activity。

Hope this helps.

希望这可以帮助。

回答by ivagarz

Activity a = (Activity) getContext();

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

只要您在构造函数中将当前活动作为上下文传递,就像您已经在做的那样。

回答by Gabriel Marques

Method this.getParent()works.

方法this.getParent()有效。