Search code examples
androidactivity-lifecycle

Activity gets re-created during the transition to a new one


In the App I'm developing I've got 2 Activities, ActA and ActB.

ActA is the first one to be displayed.

We can say ActA works like a SplashScreen.

Inside ActA i retrive some data which i need in ActB for performing some tasks.

When this data get retrived i can call an Intent which perform the switch to ActB.

Here's the problem: ActB extends ActA becouse it needs ActA retrived data to perform his tasks.

Probably becouse of that, in the moment ActB gets called, ActA method "OnCreate" gets called to.

This create a loop becouse ActA starts retriving other data and calls again ActB. So my app Crash.

How do i forbid ActA to start a second time?


Solution

  • If you don't want to exchange data between Activity you can do this.
    Create a base activity like this:

        class BaseActivity extends Activity{
          String data;
        }
    
        Now class ActA extends BaseActivity{
        //you can put value to data directly
        data="ABCD";
        }
    
        class ActB extends BaseActivity{
        //Here you can access the data string directly
        }