0

텍스트 상자와 단추가있는 활동과 사용자가 텍스트 상자에 입력 한이 이름을 사용하는 서비스가있는 응용 프로그램이 있습니다. 내가 이름을 입력하고 (버튼 클릭)에 서비스를 시작하면 아래의 코드와 같이, 나는 마무리() 활동을 파괴하는 전화 :Activity에서 finish() 메소드 사용하기

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_start); 
    Log.e("onCreate", "Activity"); 

    final Button button = (Button) findViewById(R.id.startServiceBTN); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      EditText mEdit = (EditText) findViewById(R.id.nameTB); 
      if (!hit) { 
       hit = true; 
       if (mEdit.getText().toString().trim().length() > 0) { 
        Intent intent = new Intent(StartActivity.this, BLEService.class); 
        intent.putExtra("Name", mEdit.getText().toString()); 
        startService(intent); 
        StartActivity.this.finish(); 
       } else { 
        Toast.makeText(getApplicationContext(), 
          "Please Enter a Valid Name", Toast.LENGTH_LONG) 
          .show(); 
       } 
      } 

     } 
    }); 
} 

내가 마무리를 호출하지만,이다 직면 문제()에서 Nexus 5의 뒤로 및 홈 버튼 옆에 최근 열린 애플리케이션 아이콘 트레이 (가장 오른쪽에있는 가장 부드러운 버튼)에서 애플리케이션 활동을 볼 수 있습니다.

최근 열린 애플리케이션 트레이 , 나는 빈 Text Box가있는 App Back을 얻는다. 그래서 위의 코드를 setOnClickListener 버튼 클릭 이벤트 이전의 체크로 수정하여 Activity로 돌아 왔을 때 아래 코드 스 니펫을 사용하여 사용자가 입력 한 텍스트로 텍스트 상자를 미리 채 웁니다.

if (hit) { 
     EditText mEdit = (EditText) findViewById(R.id.nameTB); 
     mEdit.setText(BLEService.NameFromActivity); 
     finish(); 
    } 

위의 조각이 null 값을 가지고 있지 텍스트 상자를 할 수 있지만은 앱 활동은 최근 앱 트레이에 다시 사용할 수 있습니다.

여기 내 문제입니다. 최근 Apps 트레이에서 응용 프로그램을 닫거나 스 와이프하여 완전히 지우는 경우 사용자가 입력 한 이름 대신 서비스 로그에서 Name 값으로 null이 시작됩니다.

답변

1
당신은 단지 매니페스트 파일에 activity 태그에 다음 줄을 추가 할 필요가

,

android:excludeFromRecents="true"

것은 true 최근 트레이에 표시에서이 Activity 제외됩니다 설정. 당신은 사용자가 입력 한 텍스트를 유지하려면

<activity 
      android:name="com.example.MainActivity" 
      android:excludeFromRecents="true" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

참조 http://developer.android.com/guide/topics/manifest/activity-element.html

+1

가치가 메모리에 반드시 활성화되지 list_ _recent 애플 리케이션에 나와있는 응용 프로그램 (이 확인하는 것이 말 : http://android.stackexchange.com/questions/47652/do-applications-that-remain -in-recent-apps-list-take-up-resources)를 사용합니다. – Trinimon

+0

@ Trrinimon : 네 말이 맞아. 고마워. –

+0

안녕하세요, 스프링 브레이커, 답장을 보내 주셔서 감사합니다. excludeFromRecents를 사용하면 로그에 NAME 대신 NULL 값만 있습니다. 나는 어떻게 든 Activity Object를 파괴하기 전에 Activity로부터 Value (NAME)를 보존 할 필요가있다. SharedPrefs를 확인하고 여기에서 업데이트합니다. –

1

, 아래를 참조하십시오, 당신은 그것을 저장 Shared Preferences를 사용할 수 있습니다.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPref.edit(); 
editor.putString("PREFERENCE_NAME", mEdit.getText().toString()); 
editor.commit(); 

또한 onCreate에서 해당 환경 설정을 가져올 수 있습니다.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
String defaultName = "John Doe"; 
String name = sharedPref.getString("PREFERENCE_NAME", defaultName); 
mEdit.setText(name); 
관련 문제