我想传递一个从一个标签到另一个标签的意图。当我按下第一个片段中的一个按钮时,我希望它将我引导到另一个片段(例如活动)。是否有另一种方法可以在类似于startActivity的片段中执行此操作(new Intent(EntryTab.this,Journals.class)); ?下面是我的代码,startActivity行显示错误。请帮忙!
public class EntryTab extends Fragment{
public static final String LOGTAG = "BALANCE";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.entries, container, false);
Button save = (Button) ll.findViewById(R.id.entriesButton);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BalanceApp myapp= (BalanceApp) getActivity().getApplicationContext();
DataSource datasource = myapp.datasource;
datasource.open();
Log.i(LOGTAG,"inside tab entry");
EditText amount = (EditText) ll.findViewById(R.id.amount);
int amt = Integer.parseInt(amount.getText().toString());
EditText debit = (EditText)ll.findViewById(R.id.debitText);
String dr = debit.getText().toString();
EditText credit = (EditText)ll.findViewById(R.id.creditText);
String cr = credit.getText().toString();
datasource.createEntry(dr, "DR", amt);
datasource.createEntry(cr, "CR", amt);
Cursor c=datasource.database.rawQuery("SELECT * FROM entries", null);
c.moveToFirst();
// Log.i(LOGTAG, dr + " " + amt + " " + cr );// the last row
Log.i(LOGTAG, c.getString(0) + " " + c.getString(1) + " " + c.getString(2) );// shows always the first row
datasource.close();
startActivity(new Intent(EntryTab.this, Journals.class)); // error: the constructor intent is undefined.
}
});
return ll;
}
}
答案 0 :(得分:0)
活动可以包含许多碎片。在片段中,您可以通过getActivity()
方法获取活动。
此外,从活动A开始 - >活动B我们使用意图。 同样,从片段A - >>片段B,在活动A中,我们使用FragmentTransactions。
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment f = new CustomFragment();
fragmentTransaction.add(R.id.fragment_container, f);
fragmentTransaction.commit();
另外,使用addToBackStack()
方法,以便在backpress上从片段B返回片段A.