简体   繁体   中英

Start new activity from a normal class method

I don't know how to write a method in a class that would start another activity.

I have a footer with 5 buttons, and every button should start a new activity. And I would like to create a class with the 5 methods that are starting the activities.

I would like to do something like that:

My Footer_buttons class:

public class Footer_buttons{

//Back to Home activity
    public static void home_footer(Context context) {   

        Intent intent = new Intent(context, Home_page.class);
        context.startActivity(intent);
    }
}

In one of my activities I would like to call something like that:

    private static Context context;
....
        context = this;
....

    public void home_footer(View view) {    
        Footer_buttons.home_footer(context);
    }

You can specify the behaviour a button should perform in a couple of different ways.

xml onClick attribute Firstly, buttons have an xml attribute called onClick. You can assign a method name to this attribute:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here"
    android:onClick="goToActivity" />

This button will call the goToActivity method in the Activity that this layout belongs to.

public void goToActivity(View view) {
 Intent i = new Intent(this,NewActivity.class);
 startActivity(i);

}

onClickListener in a fragment The following example applies an onClickListener to a button in a fragment's layout during the fragment's onCreateView event.

Here is the button in the fragment's xml:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here" />

Note that we no longer use the onClick xml attribute of the button.

The onClickListener is an interface and can be implemented as an anonymous class inside of the fragment class:

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

// Find your button in the layout.
Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton);

btnMyButton.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
        Intent i = newIntent(getActivity(),NewActivity.class);
        startActivity(i);
  }

});

onClickListener in an Activity The following example applies an onClickListener to a button in an Activity's layout during the fragment's onCreate event.

Here is the button in the fragment's xml:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here" />

Once again, we do not use the onClick xml attribute of the button.

The onClickListener interface is now implemented as an anonymous class inside of the activity class:

    // Find your button in the layout.
Button btnMyButton = (Button)findViewById(R.id.btnMyButton);

btnMyButton.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
        Intent i = newIntent(this,NewActivity.class);
        startActivity(i);
  }

});

Finding xml elements at runtime

Finding xml elements at runtime, as shown in the previous 2 examples requires that the elements are assigned an id:

android:id="@+id/btnMyButton"

and that this ID is referenced in the calling code:

R.id.btnMyButton

When an activity is looking for elements in its layout, it can call the findByView method directly, as in below:

Button btnMyButton = (Button)findViewById(R.id.btnMyButton);

When a fragment is looking for elements in its layout, it must call findViewByID on its own view first, as in below:

Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton);

Casting

Note that in both examples, the return value of findViewByID is being cast to the declared type - in this case Button.

Button btnMyButton = (Button)...

findViewByID returns a View by default - View is a parent of Button and represents the most general type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM