我的應用程序由導航抽屜和4個片段構成。 這個應用程序的舊版本使用活動,所以我需要改變片段中的活動。安卓力量當屏幕更改方向時重新繪製片段
目前一切運作良好,但在一個Fragment
我有80 Buttons
用戶可以設置文字和背景顏色和方法調用DialogActivity
是在管理所有Fragments
和onActivityResults
通過對話框稱爲內Fragments
用於管理的唯一一個MainActivity
用戶更改。
當屏幕方向更改爲橫向時,會發生此問題。 如果我按Buttons
並設置文字和顏色所有作品與肖像但如果改變方向的屏幕與風景我得到的東西像「背景」的像「陰影」和我沒有改變它們的屬性,當我點擊他們,但如果我再次旋轉屏幕變化的按鈕變得可見。 奇怪的是,在片段背景中,我看到的按鈕與正確的更新,但不是在頂部...(我發佈的照片,這很難解釋)
我改變的舊按鈕仍然改變,因爲我把它保存到數據庫,但與景觀我不能更新澈其他按鈕...
CODE:
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
static String clickedButtonViewId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (findViewById(R.id.content_frame) != null){
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new OrarioFragment()).commit();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();
if (id == R.id.nav_orario) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new OrarioFragment())
.commit();
} else if (id == R.id.nav_calendario) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new CalendarioFragment())
.commit();
} else if (id == R.id.nav_voti) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new VotiFragment())
.commit();
} else if (id == R.id.nav_registrazioni) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new RegistrazioniFragment())
.commit();
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void addMateria(View v){
/* Prendo il nome della risorsa cosi nel ricompilare il progetto non perdo *
* tutti i riferimenti ai bottoni salvati nel database */
clickedButtonViewId = getResources().getResourceEntryName(v.getId());
//StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
Intent myIntent = new Intent(MainActivity.this, ActivityAddMateria.class);
startActivityForResult(myIntent, 1);
//onStop();
}
//Take back data from ActivityAddMateria
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1) {
if (resultCode == RESULT_OK) {
MySQLiteHelper db = new MySQLiteHelper(this);
//Cambio subito il Button
int resId = getResources().getIdentifier(clickedButtonViewId, "id", getPackageName());
final Button clickedtextView = (Button) findViewById(resId);
String result = data.getStringExtra("result"); //Take the materia from Dialog
int color = data.getIntExtra("color", 1); //Take the color from Dialog
//Controllo se il Button è già presente nel db se presente aggiorno se non presente inserisco
boolean modifica = db.Exists(clickedButtonViewId);
//Se voglio ripristinare il bottone di default
if (color == getResources().getColor(R.color.blue_orario)) {
//Ripristino la grafica di Default
Drawable style = setButtonColor(color);
clickedtextView.setBackground(style);
clickedtextView.setText("New");
//Se la materia è nel database la cancello
if (modifica) {
db.deleteSingleMateria(clickedButtonViewId);
}
} else {
//Quando inserisco un normale bottone colorato
if (!modifica) {
//Materia da inserire in un nuovo spazio
db.addMateriaToDb(new Materia(clickedButtonViewId, result, color));
} else {
//Materia già presente nel Button quindi aggiorno la materia
db.updateMateria(new Materia(clickedButtonViewId, result, color));
Toast.makeText(getApplicationContext(), "Materia modificata!",
Toast.LENGTH_LONG).show();
}
//Inserisco la materia nel DB dei voti_media
db.addMateriaVotiFromOrario(new MaterieVoti(result, 0.0));
clickedtextView.setText(result);
//clickedtextView.setBackgroundColor(color);
//clickedtextView.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
Drawable style = setButtonColor(color);
clickedtextView.setBackground(style);
}
}
if (resultCode == RESULT_CANCELED) {
//Nessuna materia inserita
}
}
}//onActivityResult
編輯
好吧,我發現這個問題。
在MainActivity我有力這行代碼的第一片段中顯示
if (findViewById(R.id.content_frame) != null){
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new OrarioFragment()).commit();
}
而當屏幕方向改變在MainActivity被重建並且如果加載相同的片段通過舊片段,因爲我使用.add()
那麼我應該如何設置片段顯示,當應用程序開始避免這個問題?
我管理抽屜有問題嗎?
顯示您的代碼。 – Bryan
@Bryan添加了代碼,如果需要更多隻是告訴:) – Dario
你也可以分享你的佈局嗎? – JRG