我怎样才能从(activity1-> fragment1)回归,实际上我的设计概念就像在fragment1中的(fragment1-> activity1-> fragment1)我点击它带来的数据到activity1进行编辑或更新点击两个按钮之后的数据或删除数据它将带我回到fragment1并查看整体数据但我的问题是我点击两个按钮它将从应用程序中跳出来,我该如何解决这个问题?使用意图还是?
以下代码是Update_detail_info(activity1)
public class Update_detail_info extends AppCompatActivity implements View.OnClickListener {
EditText weight,bodyf,timef,datef,commentf;
Button btnUpdate, btnRemove;
long weight_id;
SQLControlerWeight dbcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_detail_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbcon = new SQLControlerWeight(this);
dbcon.openDatabase();
btnUpdate = (Button) findViewById(R.id.button_update);
btnRemove = (Button) findViewById(R.id.button_remove);
Intent i = getIntent();
String weightID = i.getStringExtra("weightId");
String dN = i.getStringExtra("dateNum");
String tN = i.getStringExtra("timeNum");
String wN = i.getStringExtra("weightNum");
String bN = i.getStringExtra("bodyFatNum");
String cM = i.getStringExtra("comment");
weight_id = Long.parseLong(weightID);
datef.setText(dN);
timef.setText(tN);
weight.setText(wN);
bodyf.setText(bN);
commentf.setText(cM);
btnUpdate.setOnClickListener(this);
btnRemove.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_update:
String d_update = datef.getText().toString();
String t_update = timef.getText().toString();
String w_update = weight.getText().toString();
String b_update = bodyf.getText().toString();
String c_update = commentf.getText().toString();
dbcon.updateNote(weight_id,w_update,b_update,d_update,t_update,c_update);
this.returnHome();
break;
case R.id.button_remove:
dbcon.deleteNote(weight_id);
this.returnHome();
break;
}
}
public void returnHome() { // is that here the code are wrong ? cause i need to jump back to the previous fragment1
Intent home_intent = new Intent(getApplicationContext(),
HistoryF.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home_intent);
}
}
break;
}
下面的代码是HistoryF(fragment1)的片段
public class HistoryF extends Fragment {
View contentView;
ListView list;
SQLControlerWeight dbconnection;
TextView weight_num, date_num, time_num, bf_num, comment,weight_ID;
private SimpleCursorAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contentView = inflater.inflate(R.layout.history_fragment, container, false);
dbconnection = new SQLControlerWeight(getActivity());
dbconnection.openDatabase();
list = (ListView) contentView.findViewById(R.id.listViewWeight);
Cursor cursor = dbconnection.readNote();
String[] from = new String[]{
DBHelperNote.WEIGHT_ID,
DBHelperNote.WEIGHT_NUM,
DBHelperNote.BODY_FAT,
DBHelperNote.WEIGHT_DATE,
DBHelperNote.WEIGHT_TIME,
DBHelperNote.WEIGHT_COMMENTS
};
int[] to = new int[]{
R.id.weight_id,
R.id.weight_num,
R.id.bf_num,
R.id.date_num,
R.id.time_num,
R.id.comment_text
};
adapter = new SimpleCursorAdapter(
contentView.getContext(), R.layout.history, cursor, from, to,0);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
weight_ID = (TextView) view.findViewById(R.id.weight_id);
weight_num = (TextView) view.findViewById(R.id.weight_num);
bf_num = (TextView) view.findViewById(R.id.bf_num);
date_num = (TextView) view.findViewById(R.id.date_num);
time_num = (TextView) view.findViewById(R.id.time_num);
comment = (TextView) view.findViewById(R.id.comment_text);
String weightId = weight_ID.getText().toString();
String wn = weight_num.getText().toString();
String bfn = bf_num.getText().toString();
String dn = date_num.getText().toString();
String tn = time_num.getText().toString();
String cm = comment.getText().toString();
Intent modify_intent = new Intent(getActivity(), Update_detail_info.class);
modify_intent.putExtra("weightId", weightId);
modify_intent.putExtra("dateNum", dn);
modify_intent.putExtra("timeNum", tn);
modify_intent.putExtra("weightNum", wn);
modify_intent.putExtra("bodyFatNum", bfn);
modify_intent.putExtra("comment", cm);
startActivity(modify_intent);
}
});
return contentView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
这是我控制标签
的主要活动public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new KeyInWeightF(), "TRACK");
adapter.addFragment(new HistoryF(), "HISTORY");
adapter.addFragment(new AnalysisF(), "GRAPH");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您只需在returnHome()
上使用此代码即可Intent intent = NavUtils.getParentActivityIntent(Update_detail_info.this); //get parent activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(Update_detail_info.this, intent); // and then navigate it to parent
或
NavUtils.navigateUpFromSameTask(Update_detail_info.this);
两者都是相同的
并在清单文件中定义parentActivity
的manifest.xml
<activity
android:name="com.example.yourApp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.yourApp.Update_detail_info"
android:label="@string/title_activity_Update_detail"
android:parentActivityName="com.example.yourApp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourApp.MainActivity" />
</activity>
有关提供向上导航的详细信息,请转到此link。
要了解 startactivityforresult ,请转到此link