0

I see that you can bring your activity form the background to the foreground in android using the following lines

Intent intent = new Intent(this, MainActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

Now the problem is I don't know how to get the name of the react activity to work - When I try to compile I get

error: cannot find symbol Intent i = new Intent(mReactContext, MainActivity.class);

symbol: class MainActivity

I'm not very experienced with java - so this is part of my problem, I tried using getCurrentActivity() - but this doesn't seem to work either.

Anyone has an idea?

Thanks!

2 Answers 2

1

If you have access to reactContext (I see you have) you can try this:

Context context = getReactApplicationContext();
String pn = context.getApplicationContext().getPackageName();
Intent li = context.getPackageManager().getLaunchIntentForPackage(pn);
context.startActivity(li);

String pn is just a package name (e.g. "com.yourapp"), so you can skip getPackageName() and just pass your package name to getLaunchIntentForPackager(). You can check this solution on react-native-system-notification

Sign up to request clarification or add additional context in comments.

1 Comment

This seems like a good idea, I already managed to resolve the problem though by moving this code into the android folder (I was trying to do this as an external package, to be installed by npm) - then my code works. A different problem though is that it seems that react doesn't always find the "extras" I sent with the intents :(
0

You should get reference of your first activity. Then Create your intent and start activity. Run the code below somewhere in your code(for example in a react method which triggers an activity)

  • Activity currentActivity = getCurrentActivity();
  • Intent intent = new Intent(name of your activity);
  • currentActivity.startActivityForResult(intent,FLAG_ACTIVITY_NEW_TASK );

// create onActivityResult method to process your task according to result

      public void onActivityResult(int requestCode, int resultCode, Intent data) {

      if (requestCode == FLAG_ACTIVITY_NEW_TASK ) {

             // do your job
            }
        }

It seems you use MainActivity.class as parameter in intent. I'm not an expert but it seems wrong. In a react-native project mainactivity will be triggered automatically and index.android.js will be called. You can use an activity you create as well you don't have to use native android activities like Intent.ACTION_GET_CONTENT which can be used to get information of an image from gallery.

1 Comment

In all the stack overflow questions I've seen about this they use classname.class to start activity. on the other side, I do want to try what you are saying... but I'm not sure I understand exactly what to do - how do I get the name of my activity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.