문제

Usually you would start an activity with something like the following:

private void llMenuSettings_Click() {
     this.runOnUiThread(new Runnable() {
          public void run() {
               Intent intent = new Intent(XXXXXXXX.this, View_Settings.class);
               startActivity(intent);
               XXXXXXXX.this.finish();
          }
     });
}

However I am extending a LinearLayout:

public class MenuContainer extends LinearLayout {
}

Therefore I have run into a couple of issues:

  1. How would you know what XXXXXXXX is in the example as the LinearLayout can be included in multiple Layouts?
  2. startActivity() does not exist in a LinearLayout class?

There are a few similar questions but none that fully answer the above scenario therefore hoping this can be answered and help others in the future?

도움이 되었습니까?

해결책

Inside your LinearLayout extending class, you could create the following method:

public void launch() {

    Intent i = new Intent(getContext(), YourActivity.class);
    getContext().startActivity(i);
}

Use the getContext() method inside your customview to retrieve the Context and start a new Activity.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top