I’ve built a simple customized view and registered onClickListener() in a constructor. To start a new Activity when view instance is clicked, I simply used something like this:
setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getContext(), com.test.myClass.class);
Context context = getContext();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
Since my knowledge level is still very poor, I was wandering if this is a good way to start activity from View and what would be a better way if there is one?
Thank you!
this implementation is very valid, but could be improved with two little things:
the improvement is the following: If the user clicks like crazy on your View, it can be the case that the
OnClickListeneris trigered more than once, and therefore starts multible Activitys. The boolean fixes it.Also your
OnClickListeneris always the same, though you can just declare it once inside your View and don’t instantiate them more than once.Note: You might need to set
clickedback to false at some point (onPause()of the Activity which holds your View seems to be a reasonable place)