Reputation: 1074
I have a problem with passing a number from one fragment to another in the same activity , I got that number to Activity, but when I try to pass it to 2nd fragment it gives me null. Any help is appreciated
Here is the code for the Passing and a bit more, there maybe typos as i wanted to put as little of code as I could
MainActivity
package com.example.design.ex;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import example.test.R;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.design.ex.fragments.ReceiptItemsFragment;
public class NewReceiptActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int num = 0;
Intent intent = getIntent();
int Broj = intent.getIntExtra("Num", num);
Bundle bundle=new Bundle();
bundle.putInt("Broj", Broj);
setContentView(R.layout.activity_test);
ReceiptItemsFragment frag = (ReceiptItemsFragment)
getSupportFragmentManager().findFragmentById(R.id.receipt);
frag.setArguments(bundle);
}
}
Fragment 1
package com.example.design.ex.fragments;
import example.test.R;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.actionbarsherlock.app.SherlockFragment;
public class CategoriesItemsFragment extends SherlockFragment {
int num = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_categories_items,
container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
itemsGrid.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, final View itemView, int
arg2, long arg3)
{
num = arg2 + 1;
Intent intent = new Intent(getActivity().getBaseContext(),NewReceiptActivity.class);
intent.putExtra("Num", num);
startActivity(new Intent(getActivity(), NewReceiptActivity.class));
getActivity().startActivity(intent);
}
});
}
}
Fragment 2
package example.design.ex.fragments;
import example.test.R;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
public class ReceiptItemsFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
Bundle arguments = getArguments();
if (arguments != null)
{
Log.d("ISnull","no!");
} else {
Log.d("ISnull","yes!");
}
int num=getArguments().getInt("Num");
return inflater.inflate(R.layout.fragment_receipt_items, container,
false);
}
Main Activity XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/titles"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1.5"
class="example.ex.fragments.CategoriesItemsFragment" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/sun_flower" >
</LinearLayout>
<fragment
android:id="@+id/receipt"
android:name="example.ex.fragments.ReceiptItemsFragment"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
class="example.ex.fragments.ReceiptItemsFragment" />
</LinearLayout>
Here is the Error
06-11 07:41:00.419: E/AndroidRuntime(2801): at
com.example.NewReceiptActivity.onCreate(NewReceiptActivity.java:60)
which is this line or the next if i put log (tried to get it to show me the id)
ReceiptItemsFragment frag = (ReceiptItemsFragment)
getSupportFragmentManager().findFragmentById(R.id.receipt);
Upvotes: 0
Views: 1630
Reputation: 1074
I went on different approach using Interface on passing data,As explained here
https://stackoverflow.com/a/12311939/1393695
And got it working, I want to thank everybody here for the help.
The problem was that I was looking at fragments as they where activities.
Upvotes: 0
Reputation: 4698
I think the problem is here :
ReceiptItemsFragment frag=new ReceiptItemsFragment(); //2nd fragment
frag.setArguments(bundle);
You're creating a new fragment but its not the one you've got in your layout.
try this instead :
setContentView(R.layout.activity_test); // Here is where that 2 fragments are Located
ReceiptItemsFragment frag = getFragmentManager().findFragmentById(...) //your fragment id
frag.setArguments(bundle);
so you don't create a new fragment but reference the one you've got in your layout.
Hope that helps.
Upvotes: 1
Reputation: 609
Verify if frag
in NewReceiptActivity
is different null. Verify if Numt
is 2
.
Move the line in ReceiptItemsFragment:
int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null
from method onCreateView to onActivityCreated:
public void onActivityCreated (Bundle savedInstanceState) {
int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null
Upvotes: 1