如何创建包含要在intent

时间:2015-10-06 14:58:58

标签: java android android-intent android-studio

我一直在网上搜索关于一个可以包含类对象的变量的线索,但我什么也没找到。我相信有一些方法可以将一个类的信息存储在一个要在intent中使用的变量中。对不起的解释感到抱歉,这里是代码来说明我的意思。

MainActivity.class

public class MainActivity extends Activity {

    Button startBtn, chooseLevelBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startBtn = (Button) findViewById(R.id.startBtn);
        chooseLevelBtn = (Button) findViewById(R.id.chooseLevelBtn);

        class classVariable = "Level001Activity.class";
        /*
         * I want a variable like this where I can store the Activity.class
         * So that I can use a function to determine the last level played,
         * and so when the player press playBtn, it would automatically
         * send the intent to the latest level
         * Now, how do I achieve this ?
         */

        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(),
                    classVariable);
                startActivity(i);
            }
        });
    }
}

P.S我将从我存储在设备内部存储器中的json文件中获取最后一级播放信息。或者非常感谢任何更好的方法。提前谢谢。

1 个答案:

答案 0 :(得分:4)

是的,你可以这样做。您需要将其设为Class而不是class并删除引号,因为它不应该是String

Class<? extends Activity> classVariable = Level001Activity.class;
相关问题