Вопрос

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