how to change actionBar title of Activity1 from a button of Activity2

Henry :

I am trying to make a button in activity2(settings page), when the button is clicked, then the title of activity1(MainActivity) will change to what I set to. I have been trying to use interface to carry out the function but it gives me a null pointer exception error.

MainActivity

@Override
    public void changeActionBarTitle(String editText){
        actionTitle = editText;
        setTitle(actionTitle);
    };

Interface

public interface ActionBarInterface {
    void changeActionBarTitle(String editText);
}

Setting page (activity 2)

public class SettingsPage extends AppCompatActivity {
    ActionBarInterface actionBarInterface;
    Button editCompanyNameButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_page);
        setTitle("Settings");

        editCompanyNameButton = findViewById(R.id.editCompanyNameButton);
        editCompanyNameButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                actionBarInterface.changeActionBarTitle("test");
            }
        });
    }
}

Thanks.

tomysek :

You can try this code without using the interface

MainActivity:

    @Override
protected void onResume() {
    setTitle(Main2Activity.title);
    super.onResume();
}

activity2:

public class SettingsPage extends AppCompatActivity {
Button editCompanyNameButton;
static String title;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    setTitle("Settings");

    editCompanyNameButton = findViewById(R.id.editCompanyNameButton);
    editCompanyNameButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            title = "test";
        }
    });
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=217191&siteId=1