ChooseLanguageFragment.java
Intent explicitGetNumberServiceIntentUSA = new Intent(getActivity(), GetNumberService.class);
explicitGetNumberServiceIntentUSA.putExtra("country", "USA");
getActivity().startService(explicitGetNumberServiceIntentUSA);
This is how the GetNumberService is called.
This is how the arraylist is passed from GetNumberService.java
Intent mobileNumbersIntent = new Intent();
mobileNumbersIntent.putStringArrayListExtra("mobileNumbers", mobileNumberList);
mobileNumbersIntent.setAction(ChooseNumber1.MobileNumberBroadcastReceiver.MOBILE_NO_RECEIVER);
mobileNumbersIntent.addCategory(Intent.CATEGORY_DEFAULT);
mobileNumbersIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is needed when callling startActivity() outisde an Activity
sendBroadcast(mobileNumbersIntent);
in onHandleIntent() method.
GetNumberService does its job perfectly.
ChooseNumber1.java
public class ChooseNumber1 extends AppCompatActivity {
private MobileNumberBroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter intentFilter = new IntentFilter(MobileNumberBroadcastReceiver.MOBILE_NO_RECEIVER);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MobileNumberBroadcastReceiver();
registerReceiver(receiver, intentFilter);
setContentView(R.layout.activity_choose_number1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onDestroy(){
unregisterReceiver(receiver);
super.onDestroy();
}
public class MobileNumberBroadcastReceiver extends BroadcastReceiver {
public static final String MOBILE_NO_RECEIVER = "abcd";
ArrayList<String> mobileNumbersList = new ArrayList<>();
ListView mobileNumberListView;
ArrayAdapter<String> mobileNumberAdapter;
@Override
public void onReceive(Context context, Intent intent) {
// if(intent.getAction().equals(GetNumberService.MOBILE_NUMBER_LIST_PASS_ACTION)){
mobileNumbersList = intent.getStringArrayListExtra("mobileNumberList");
// }
mobileNumberAdapter = new ArrayAdapter<String>(new ChooseNumbers(),
R.layout.list_item_numbers, R.id.list_item_number_textview, mobileNumbersList);
mobileNumberListView = (ListView) findViewById(R.id.listViewNumberList);
mobileNumberListView.setAdapter(mobileNumberAdapter);
}
}
}
activity_choose_number1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.a2ndnumber.a2ndnumber.ChooseNumbers">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_choose_number1" />
</android.support.design.widget.CoordinatorLayout>
content_choose_number1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.a2ndnumber.a2ndnumber.ChooseNumber1"
tools:showIn="@layout/activity_choose_number1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewNumberList"/>
</RelativeLayout>
PS 2:
Problem is, I am not able to receive the mobileNumbersIntent in ChooseNumber1.java or ChooseNumbersFragment.java. I think the issue is with BroadcastManager.java. If I debug, it goes to Handler.java and few other classes and finally mId=-1 and no error in stack trace. I am struck
PS: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a2ndnumber.a2ndnumber">
<!-- TODO : Add permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Contacts -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ChooseLanguage"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".Choose_Country"
android:label="@string/app_name"
android:parentActivityName=".ChooseLanguage"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.a2ndnumber.a2ndnumber.ChooseLanguage" />
</activity>
<activity
android:name=".MainActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.GetNumberService" />
<activity
android:name=".ChooseNumber1"
android:label="@string/title_activity_choose_number1"
android:parentActivityName=".Choose_Country"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.a2ndnumber.a2ndnumber.Choose_Country" />
</activity>
</application>
</manifest>
PS: I have modified my code a lot after going through tons of examples and answers in Stackoverflow. This is my latest code. Look like the issue is with intent-filter and/or getting intent using action. Please help me. Thanks.