2012-01-18 21 views
6

Tôi có một ứng dụng bao gồm hai hoạt động/màn hình và một lớp java mà từ đó tôi tạo ra các đối tượng.Làm thế nào để làm cho một đối tượng có thể truy cập vào tất cả các hoạt động trong một chương trình Android?

Tôi cần sử dụng đối tượng tôi đã tạo trên hoạt động đầu tiên (instanciating lớp .java) trên hoạt động thứ hai.

Cách đơn giản nhất để thực hiện việc này là gì? Tôi googled về nó và thực hiện các giao diện Parcelable trên lớp java có vẻ là câu trả lời phổ biến nhất cho vấn đề này. Nhưng đối tượng là loại phức tạp và chuyển tải từng thành viên đơn lẻ của nó có vẻ giống như một giải pháp brute-force.

Không có giải pháp thanh lịch cho điều này?

Cảm ơn

EDIT: Lưu trữ dữ liệu đối tượng trên một cơ sở dữ liệu SQlite đơn giản một giải pháp?

Trả lời

3

Bạn có thể sử dụng bối cảnh ứng dụng (mặc dù điều này về cơ bản là lưu trữ trạng thái toàn cầu, do đó bạn cần phải cẩn thận như thế nào bạn đang sử dụng nó):

Using Application context everywhere?

+0

+1, Đây cũng là phương pháp lựa chọn của tôi. Rất linh hoạt khi bạn có thể viết getters của riêng bạn một người định cư trong một lớp Ứng dụng tùy chỉnh. – Guillaume

2

Một trong những cách dễ nhất để chuyển thông tin giữa hoạt động là sử dụng Gói.

Bạn có thể thêm extras vào Intent trước khi khởi chạy Hoạt động mới.

Ở đầu bên kia (các hoạt động khác) sau đó bạn có thể nhận được những extras ra khỏi Bundle và có thể xây dựng lại các đối tượng trong các hoạt động mới ..

Dưới đây là một ví dụ:

Trước tất cả, đây là lớp sinh viên:

Student class - Student.java:

package com.stephendiniz.objectsharing; 

public class Student 
{ 
    int id; 
    String name; 
    String profession; 

    Student() 
    { 
     id = 0; 
     name = ""; 
     profession = ""; 
    } 

    Student(int id, String name, String profession) 
    { 
     this.id = id; 
     this.name = name; 
     this.profession = profession; 
    } 

    public int getId()    { return id; } 
    public String getName()   { return name; } 
    public String getProfession() { return profession; } 
} 

Các Hoạt động chính sẽ tham khảo các lớp sinh viên để tạo ra một đối tượng s

Hoạt động chính - AndroidObjectSharing.java:

package com.stephendiniz.objectsharing; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class AndroidObjectSharingActivity extends Activity 
{ 
    TextView id; 
    TextView name; 
    TextView profession; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     id = (TextView)findViewById(R.id.id); 
     name = (TextView)findViewById(R.id.name); 
     profession = (TextView)findViewById(R.id.profession); 

     Button submitButton = (Button)findViewById(R.id.submitButton); 
     submitButton.setOnClickListener(new View.OnClickListener() 
      { 
      final Intent newActivity = new Intent(AndroidObjectSharingActivity.this, NewActivity.class); 
       public void onClick(View v) 
       { 
        Student s = new Student(Integer.parseInt(id.getText().toString()), name.getText().toString(), profession.getText().toString()); 
        newActivity.putExtra("extraStudentId", s.getId()); 
        newActivity.putExtra("extraStudentName", s.getName()); 
        newActivity.putExtra("extraStudentProfession", s.getProfession()); 

        startActivity(newActivity); 
       } 
      }); 
    } 
} 

Đây là XML được gắn với lớp sinh viên:

Bố cục XML chính - main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Student ID:" /> 
    <EditText 
     android:id="@+id/id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="9001" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Student Name:" /> 
    <EditText 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Steve" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Student Profession:" /> 
    <EditText 
     android:id="@+id/profession" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Computer Engineer" /> 

    <Button 
     android:id="@+id/submitButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="30dp" 
     android:text="Send to New Activity" /> 

</LinearLayout> 

Lưu ý rằng NewActivity truy cập vào thông tin từ một Bundle (tên infoBundle trong trường hợp này) ..

Đối tượng là "xây dựng lại" lập trình như thể nó đã được thông qua.

Hoạt động mới - NewActivity.java:

package com.stephendiniz.objectsharing; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class NewActivity extends Activity 
{ 
     LinearLayout ll; 

     TextView id; 
     TextView name; 
     TextView profession; 

     private final static String TAG = "NewActivity"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     Bundle infoBundle = getIntent().getExtras(); 
     Student s = new Student(infoBundle.getInt("extraStudentId"), infoBundle.getString("extraStudentName"), infoBundle.getString("extraStudentProfession")); 

     ll = new LinearLayout(this); 
     ll.setOrientation(LinearLayout.VERTICAL); 

     id = new TextView(this); 
     name = new TextView(this); 
     profession = new TextView(this); 

     id.setText("Student Id: " + s.getId() + "\n"); 
     name.setText("Student Name: " + s.getName() + "\n"); 
     profession.setText("Student Profession: " + s.getProfession() + "\n"); 

     ll.addView(id); 
     ll.addView(name); 
     ll.addView(profession); 

     Button closeButton = new Button(this); 
     closeButton.setText("Close Activity"); 
     closeButton.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        finish(); 
       } 
      }); 

     ll.addView(closeButton); 

     setContentView(ll); 
    } 
} 

Đừng quên để thêm hoạt động mới để Manifest của bạn!

Android Manifest - AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.stephendiniz.objectsharing" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="15" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".AndroidObjectSharingActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".NewActivity" 
      android:label="New Activity" /> 
    </application> 

</manifest> 

Xin lỗi vì sự pha trộn của XML và theo chương trình Scripting, tôi nhận ra sau khi Hoạt động đầu tiên mà nó sẽ có rất nhiều các tập tin để hiển thị vì vậy tôi ngưng tụ các tệp cuối cùng, nhưng bạn sẽ có thể có được ý tưởng ..

Hy vọng điều này sẽ hữu ích!

0

Tôi nghĩ Singleton là cách phù hợp để tạo đối tượng có thể truy cập trong mọi hoạt động. Xem câu trả lời this để biết thêm thông tin về cách hoạt động của nó.

Các vấn đề liên quan