How to call another activity method in android studio?

风格不统一 提交于 2021-01-28 07:52:33

问题


I have two different activities. The first calls the menu(base) if the user is logged in, but have also the method for display the user information.

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    UserLocalStore userLocalStore;
    EditText etName, etAge, etUsername;
    Button bLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etUsername = (EditText) findViewById(R.id.etUsername);
        etName = (EditText) findViewById(R.id.etName);
        etAge = (EditText) findViewById(R.id.etAge);
        bLogout = (Button) findViewById(R.id.bLogout);

        bLogout.setOnClickListener(this);

        userLocalStore = new UserLocalStore(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bLogout:
                userLocalStore.clearUserData();
                userLocalStore.setUserLoggedIn(false);
                Intent loginIntent = new Intent(this, Login.class);
                startActivity(loginIntent);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (authenticate() == true) {
            startActivity(new Intent(this, Base.class));
        }
    }

    private boolean authenticate() {
        if (userLocalStore.getLoggedInUser() == null) {
            Intent intent = new Intent(this, Login.class);
            startActivity(intent);
            return false;
        }
        return true;
    }

    public void displayUserDetails() {
        User user = userLocalStore.getLoggedInUser();
        etUsername.setText(user.username);
        etName.setText(user.name);
        etAge.setText(user.age + "");
    }
}

The second activity is the menu; this activity has a button called "bUtente" that when clicked must show the user info.

public class Base extends Activity implements View.OnClickListener {
    Button bDiario;
    Button bUtente;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        bDiario = (Button) findViewById(R.id.bDiario);
        bDiario.setOnClickListener(this);
        bUtente = (Button) findViewById(R.id.bUtente);
        bUtente.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bDiario:
                startActivity(new Intent(this, Diary.class));

            case R.id.bUtente:
                MainActivity prova = new MainActivity();
                prova.displayUserDetails();
        }
    }
}

How can I do that when I click the button "bUtente" reminds me of the other activity by performing the method "displayUserDetails()"?


回答1:


MainActivity has a layout that contains the Views that show the user information. If you are in Base, that Activity has a different layout that doesn't have any Views for showing the user information. You can create a Dialog that shows the user information, or you could add additional Views to Base that would show the user information, or you could rearchitect your application so that you have another Activity which shows the user information and both MainActivity and Base could start that Activity to show the user information.

In any case, you absolutely positively cannot instantiate an Activity like MainActivity using the new keyword! Only Android can instantiate Activity components, because the framework sets up the underlying Context when it does that. It also keeps track of these components and manages the lifecycle callbacks.



来源:https://stackoverflow.com/questions/32101237/how-to-call-another-activity-method-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!