-1

i need to get insert method which is exist in warehouse activity to another activity to insert object of warehouse entity i do'nt try to open WareHouseActivity just call insert method from activity in my activity to pass object of it

i use this two method but the problem doesn't solve i know why this methods are uncorrect but i put them for example


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

WareHouseentity warehouseentity=new WareHouseentity();

method 1 ;;;;;;;;;;;;;
((WarehouseActivity)getApplicationContext).insert(warehouseentity);

method 2;;;;;;;;;;
WareHouseActivity warehouseactivity=new WareHouseActivity();
warehouseactivity.insert(warehouseentity)

}

  • This is a wrong approach . `insert` method hold business logic so it should be separated from the UI layer . Create a separate class to handle all Crud actions then u can just call the method of that class from anywhere by creating Object or by using Singleton Object . these things are way too easy if You follow some pattern like `MVVM` .. – ADM Apr 04 '22 at 05:24
  • @ADM can you tell me how to make it please i try but cannot get method because context of activity which i try get insert method from it – شاكر نيال Apr 05 '22 at 09:32

1 Answers1

1

You need to create object of your Activity, By using this Object you are able to use activity method.

For more detail; visit this link. How to use method from another Class?

Use this to call your insert action.

Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
  • That's totally wrong you don't create an Object of `Activity` never .. And passing a static instance around is also a big No no .. This can cause several problem like Memory leak and lifecycle related crashes .. – ADM Apr 04 '22 at 05:01
  • @ADM , agree with this. Can you please guide us to achieve this with a right way!? – Bhoomika Patel Apr 04 '22 at 05:21