2015-10-27 26 views
3

以最佳方式通过意图发送IntDef的任何想法?IntDef通过意图

PS:如果你只是把它作为一个int你失去类型检查,这是具有intdef

+0

基本上,使用它作为一个int并通过辅助方法转换为您的IntDef。 帮助阅读,http://stackoverflow.com/a/32160715/794088&http://stackoverflow.com/a/31737425/794088 – petey

回答

0

你IntDef的主要目标不是在运行时可用。要走的路是将意图的生成封装在您的意图接收活动中。

public class MyActivity extends Activity { 

    private static final String KEY_NAVIGATION_MODE = "navigation_mode"; 
    private @NavigationMode int mNavigationMode; 

    public static Intent getStartIntent(Context context, @NavigationMode String navigationMode) { 
     Intent intent = new Intent(context, MyActivity.class); 
     intent.putExtra(KEY_NAVIGATION_MODE, navigationMode); 
     return intent; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //noinspection WrongConstant Safe because we saved it in #getStartIntent 
     mNavigationMode = getIntent().getIntExtra(KEY_NAVIGATION_MODE, NAVIGATION_MODE_YOUR_DEFAULT); 
    } 
}