c# - 使用意图方法调用活动
问题描述
我知道 Android 操作系统需要 paramterles 构造函数来重新创建 Activity,如果需要,我可以使用 bundle 传递一些参数,如下所示:
private void OpenOtherActivityWindow_Click(object sender, EventArgs e)
{
Intent nextActivity = new Intent(this, typeof(ThirdActivity));
Dog mydog = new Dog("mydogName");
Bundle bundle = new Bundle();
bundle.PutSerializable("mydoggy", mydog);
nextActivity.PutExtra("RowID", Convert.ToString(10));
nextActivity.PutExtras(bundle);
StartActivity(nextActivity);
}
[Activity(Label = "ThirdActivity")]
public class ThirdActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.third);
//Receive values if any from previous activity
if (!Intent.HasExtra("mydoggy")) return;
Dog tryme = (Dog)Intent.GetSerializableExtra("mydoggy");
if (!Intent.HasExtra("RowID")) return;
string text = Intent.GetStringExtra("RowID") ?? "0";
}
}
尽管如此,是否有可能创建静态方法,该方法会从给定的参数为我返回意图,例如?:
static Intent CreateIntent(Dog dog, int rowID)
如果是这样,有人可以告诉我,那么它看起来与我的代码中显示的内容相反。
解决方案
我不知道你的细节ThirdActivity
,但我可以通过创建一个简单的演示来实现类似的功能。你可以在这里查看代码。
[Activity(Label = "MovieDetailActivity")]
public class MovieDetailActivity : Activity
{
public TextView textView;
public static MovieModel mMoviemodel;// define your model here
public static int mRowID; // define a int variable mRowID
public static Intent createIntent(Context context, MovieModel movie, int rowID)
{
Intent intent = new Intent(context, typeof(MovieDetailActivity));
//Pass parameters here
mMoviemodel = movie;
mRowID = rowID;
return intent;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.detaillayout);
textView = FindViewById<TextView>(Resource.Id.info_textview);
textView.Text = "movie name:" + mMoviemodel.mMovieName + " text = " + mRowID;
}
}
用法:
// pass your Object model
StartActivity( MovieDetailActivity.createIntent(this, movie,10));
推荐阅读
- python - 如果 name/id 是前几年的条目,如何检查每个时间序列条目?
- python - Python / pynput:如何在变化时获得相对鼠标位置?
- html - 如何在使用表单和脚本发送电子邮件后隐藏确认屏幕
- sql - FROM 子句中的 Delphi 语法错误,但是 - 没有 FROM 子句
- python - 在浏览器的本地主机上显示从服务器发送到客户端的内容
- python - 熊猫在x轴上绘制时间戳 - 刻度单位?
- java - onOptionsItemSelected() 方法不会在 Fragment 上被调用
- python-3.x - 我们如何使用 Python 或 Flask 通过网络浏览器播放 IP 摄像机的实时流(RTSP 链接)?
- java - 创建具有更大对象标头的自定义 JVM
- java - e(fx)clipse css 自动完成功能不起作用且属性未知