2016-05-27 12 views
1

Ich versuche auf ProfilFragment-Seite zu gehen, aber wenn ich versuche, die Schaltfläche auf der Aktionsleiste zu drücken, geht es LoginActivity, was eine andere unabhängige Aktivität ist. Wie kann ich das durchführen? HierWie gehe ich zu einer anderen Seite, wenn ich die Zurück-Taste in der Aktionsleiste drücke?

ist der Code: Mein SettingsActivity.java

import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.support.v4.app.NavUtils; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.AppCompatActivity; 
import android.util.TypedValue; 
import android.view.Gravity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.Calendar; 


public class SettingsActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     Typeface ubuntu_MI = Typeface.createFromAsset(getAssets(), "Ubuntu-MI.ttf"); 

     // Get the ActionBar 
     android.support.v7.app.ActionBar ab = getSupportActionBar(); 

     // Create a TextView programmatically. 
     TextView tv = new TextView(getApplicationContext()); 

     // Create a LayoutParams for TextView 
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, // Width of TextView 
       RelativeLayout.LayoutParams.WRAP_CONTENT); // Height of TextView 

     // Apply the layout parameters to TextView widget 
     tv.setLayoutParams(lp); 

     // Set text to display in TextView 
     // This will set the ActionBar title text 
     tv.setText("Ayarlar"); 

     // Set the text color of TextView 
     // This will change the ActionBar title text color 
     tv.setTextColor(Color.parseColor("#FFF5EE")); 

     // Center align the ActionBar title 
     tv.setGravity(Gravity.CENTER_HORIZONTAL); 

     // Set the serif font for TextView text 
     // This will change ActionBar title text font 
     tv.setTypeface(ubuntu_MI); 

     // Underline the ActionBar title text 
     // tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG); 

     // Set the ActionBar title font size 
     tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24); 

     // Display a shadow around ActionBar title text 
     //tv.setShadowLayer(
     //  1.f, // radius 
     // 2.0f, // dx 
     // 2.0f, // dy 
     // Color.parseColor("#FF8C00") // shadow color 
     //); 

     // Set the ActionBar display option 
     ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

     // Finally, set the newly created TextView as ActionBar custom view 
     ab.setCustomView(tv); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 

     //Action Bar'daki back butonuna basınca ne yapacağını söylüyoruz. 
     if (item.getItemId() == android.R.id.home) { 
      Intent intent = new Intent(getApplicationContext(), ProfileFragment.class); 
      NavUtils.navigateUpTo(this, intent); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    } 
+1

Sie können keine Absicht erstellen, die zu einem Fragment führt, können Sie nur auf Aktivitäten navigieren. – Egor

+0

also, wie kann ich das beheben? –

+0

Erstellen Sie eine Absicht für die Aktivität, die das ProfileFragment enthält. – Egor

Antwort

0

Sie können keine Intent erstellen, die auf einem Fragment verweist. Von SettingsActivity sollten Sie einen Intent erstellen, um zu Ihrem CombineFragmentActivity zu wechseln, der ProfileFragment hostet.

Statt zu schreiben

Intent intent = new Intent(getApplicationContext(), ProfileFragment.class); 

sollten Sie schreiben

Intent intent = new Intent(this, CombineFragmentActivity.class); 

in Ihrer onOptionsItemSelected Funktion.

+0

Okay, ich habe es aber ich weiß nicht, wie es in Codes zu tun ist –

+0

Wie kann ich ProfileFragment in CombineFragmentActivity hosten? –

+0

Das sollte Ihnen helfen - https://developer.android.com/training/basics/fragments/creating.html – jaibatrik

0

Ich glaube, Sie sprechen über die Navigation nach oben. In diesem Fall können Sie eine Elternaktivität für Ihre aktuelle Aktivität angeben, die Sie immer dorthin führt.

Von Android Dokumentation:

https://developer.android.com/training/implementing-navigation/ancestral.html

<activity 
     android:name="com.example.myfirstapp.DisplayMessageActivity" 
     android:label="@string/title_activity_display_message" 
     android:parentActivityName="com.example.myfirstapp.MainActivity" > 
     <!-- Parent activity meta-data to support 4.0 and lower --> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.myfirstapp.MainActivity" /> 
    </activity> 
+0

Soll ich diese Codes in AndroidManifest.xml hinzufügen, oder? –

+0

Ja. Siehe den Link für weitere Details. –

+0

Ich schaute auf den Link und ich sah diese Zeile: Ich arbeite an 4.0.0 und oberen –