简体   繁体   中英

Getting a value from a class extending activity in a plugin

When jumping from Activity to activity via Cordova, plugin, i want to be able to put additional information into a bundle and pass it along with the intent. I seem to have issues getting the current activity and then getting a value via

public int getCounter(){return counter;}

I have a Activity Definition which, onCreate will set counter to a value from the passed in bundle.

I have a Cordova plugin then which i am working with, which will carry out the intent to next activity.

Echo Class is a mediary which will jump between acticities based on html clicks.

//In the Class:   public class Echo extends CordovaPlugin
private void launchSecondActivity(String data, CallbackContext cc){
    Bundle b = new Bundle();
    int tmp = ((SecondaryActivity)cordova.getActivity()).getCounter();
    tmp++;
    b.putInt("id", tmp);
    Intent i = new Intent(cordova.getActivity().getApplicationContext(), SecondaryActivity.class);
    i.putExtras(b);
    cordova.getActivity().startActivity(i);

    cc.success();
}

It seems that it causes a seg fault of sorts, when i am trying to assign counter to tmp, in the line:

int tmp = ((SecondaryActivity)cordova.getActivity()).getCounter();

Is there something i am doing wrong? I am trying to get the currently active activity, and then call a public function in that activity.

End Goal: I am trying to take an int, and keep passing it into intents, incremented. So that way, it will know how deep into the activity chain it is. I am working on nested state saving and curious as to the depth i am at, at any given time. The tmp being passed into the activity will be incremented each time, so it will maintain a depth.

Instead of creating a class with get counter, storing the int in the Bundle and passing it to the next activity would be far easier.

Example:

//send item to plugin.
int item = 0; // or whatever it is
Bundle b = new Bundle();
b.put("ident", item);
Intent i = new Intent();
i.putExtras();
//...

And then on the next activity.

public void onCreate(Bundle savedInstanceState)
{
    int item = getIntent().getExtras().getInt("ident");
}

Then you can do with it what you will. The answer overall while the previous activity as the old number. What does that mean? It means that you can increment and do whatever you want with it when passing it to the next activity. It is copied, so you can incrememnet and store it recursively. Allowing you to even say something like:

if (item ==0){/*set new source for Activity*/}
else if(item == 1){/*set source 2*/}
//etc.

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