简体   繁体   中英

Is there a solution that access MainActivity from other Activity class?

I want to call function below.

mobileView.loadUrl("javascript:setUserId () ");

And mobileView is in MainActivity.

public class MainActivity extends AppCompatActivity {
    private WebView mobileView;
}

I want to call function above after SecondActivity is finished.

public class SecondActivity extends AppCompatActivity {
    ...
    private void getUserId () {
        ...
        finish();
    }
}

And SecondActivity starts from AReceiver.

public class AReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        ...
        Intent i = new Intent(context, SecondActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ...
        context.startActivity(i);
    }
}

And AReceiver is called by below code...

class MainActivity {
    ...
    private void userId () {
        ...
        intent = new Intent(MainActivity.this, AReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(
                MainActivity.this, 
                alarmID, 
                intent, 
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        alarmManager.set(
                AlarmManager.RTC_WAKEUP, 
                mCalendar.getTimeInMillis(), 
                pendingIntent
        );
        ...
    }
    ...
}

But I don't know how to access mobileView from SecondActivity. Is there any solution?


What I've tried...

  1. change variable from
private webView mobileView 

to

public webView mobileView

2. call from SecondActivity Using

MainActivity.mobileView

It doesn't work for me.

You can try This option

Intent

..............

You opened the new activity from another activity with startActivityForResult . In that case you can just call the f inishActivity() function from your code and it'll take you back to the previous activity.

it work only if you start the second activity from main activity

I solved this problem using local broad cast manager: .D Thank you @Shripad Jadhav who help this answer.

Below description is solution. Hope it helpes somebody: :D


MainActivity.java

public class MainActivity extends AppCompatActivity {
    private WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        LocalBroadcastManager.getInstance(this)
            .registerReceiver(mBroadcastReceiver, new IntentFilter("webview-filter"));
        ...
    }
    ...
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mv.loadUrl("javascript: loadMainData ()");
        }
    };
    @Override
    protected void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();
    }
}

SecondActivity.java

public class SecondActivity extends AppCompatActivity {
    private void someFunction () {
        sendMessage ();
    }
    ...
    private void sendMessage() {
        Log.d("sender", "Broadcasting message");
        Intent intent = new Intent("webview-filter");

        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
    ...
}

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