简体   繁体   中英

invoke a method from activity in another

i know that this question was asked before but i had tried all the solution and get Error

i have two activity on android studio ...

the first called 'MainActivity' and contain a method ' deleteFromArrayList() ' the secound on called 'DeletButtonActivity' and contain a method ' delete(View v) '

i want to invoke 'deleteFromArrayList ()' wihtout creating another class or make the method static .... becouse i have an ArrayList inside deleteFromArrayList()

note : i send value of index i want to delete from array list using Intent ..the code in DeleteButtonActivity is public void delete(View v) {

    try {

        Intent i = new Intent(DeleteButton.this, MainActivity.class);
        i.putExtra("index", (int) spinner2.getSelectedItemId());

        (new MainActivity()).DeletButtonActivity();

        Toast.makeText(getApplicationContext(), "it was deleted", Toast.LENGTH_SHORT).show();



    }
    catch(Exception e){
        Toast.makeText(getApplicationContext(), e+"", Toast.LENGTH_SHORT).show();


    }


}

and the code in MainActivity

  public void deleteFromArrayList (){

    this.arrayList.remove(getIntent().getIntExtra("index",-1));

 }

when i run the app i got an Error NullPointerException,,, can anyone help me ..please hope that i describe the problem very well

You must not do this. There are mechanism to communicate between activities or fragments.

On can be, using startActivityForResult , this is Activity A calls Activity B, then in B you do something, and communicate the result back to Activity A.

You can have another workaround to what you want. If you can access the data in both of your activities, you can modified in ether one of them, when the activity starts, it will show the updated data.

Please first read well about an Activity here , and also provides more context of your question.

Activities in Android are no just a simple class but they also have a Lifecycle :

An activity has essentially four states:

If an activity is in the foreground of the screen (at the top of the stack), it is active or running. If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused.

A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

So the problem with your code is that when you want to access the array in a stopped activity, the instance you have been using before might not be alive anymore.

How to solve your problem

A very simple approach is to use parameter passing before you do the transition from one activity to the other, for this you'd pass your Array as an intent extra and then you "get the result back" when you finish the second activity by using onActivityResult() callback .

A second approach could be to use a Service that is something similar to an Activity but it has no UI and it has its own lifecycle. Being able to be alive even when you app it is not. Using a Service, you'll keep the Array inside the service and you'll communicate with the array to do the usual operations.

A third approach could be to use an EventBus . A very simple communication mechanism between Activities, Fragments, Threads, Services. There's a great talk titled Android Application Architecture on Android Dev Summit 2015 that uses EventBus as a communication mechanism and to implement a MVC architecture pattern on a REST Android App.

Back to your question. If you just need to 'share' an array between two activities, use the first approach. The second and third are just examples of different alternatives for the case you need a lot more than that.

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